Call a Function from within a Function?

Started by SpellSword, November 09, 2007, 04:18:19 PM

Previous topic - Next topic

SpellSword

Can you have a Function call inside of a Function?

Example:
PlayBASIC Code: [Select]
; Function One Call
FOne()

; Function One
Function FOne()
FTwo()
EndFunction


; Function Two
Function FTwo()
EndFunction

When I dream,
I carry a sword in one hand,
a gun in the other...

kevin

#1
QuoteCan you have a Function call inside of a Function?

 Yes, you can Call other functions + psubs from within a function.


PlayBASIC Code: [Select]
; Function One Call
FOne()

print "End Of Program"
Sync
waitkey

; Function One
Function FOne()
Print "function one"
FTwo()
EndFunction


; Function Two
Function FTwo()
Print "function two"
EndFunction





SpellSword

Thanks for answering that Kevin. I was afraid that, even if it was possible, I'd generate a memory overflow or something.  :-[

Another question appears!
Is there a limit to the number of Functions that can be called from within another Function?
Example 1 (Several in sequence)

[pbcode]One()

Function One()
Two()
Three()
Four()
Five()
EndFunction

Function Two()
EndFunction

Function Three()
EndFunction

Function Four()
EndFunction

Function Five()
EndFunction[/code]
Example 2 (Several in Layers)
[code]One()

Function One()
Two()
EndFunction

Function Two()
Three()
EndFunction

Function Three()
Four()
EndFunction

Function Four()
Five()
EndFunction

Function Five()
EndFunction[/pbcode]


And a slightly off topic note:
I just re-read the Function/Psub PB Help tutorials. I didn't realize that the same Functions & Psubs can be called from inside the function/Psub that is being called!  :o
Example
PlayBASIC Code: [Select]
One()

Function One()
One()
EndFunction


Interesting! So far I like to program in a modular style (Heavy on the use of Functions), so this is excellent!

I know that this is probably common knowledge, but I find the possibilities that learning of this has opened up exciting!  :)



When I dream,
I carry a sword in one hand,
a gun in the other...

nrasool

Hey SpellSword, Modular programming is great IMO, it is soo much tider and readable, however everyone has their own style. But this way, at least you breaking up portion of your program, so it makes it much easier to developer, than trying everything at once

kevin

#4
SpellSword.

  I must say i'm very impressed with your commitment to learning.  

QuoteIs there a limit to the number of Functions that can be called from within another Function?

   No, you can call as many functions as you like from within a function.    As per your first example.    

   In your second example, where each function call is nested,  each function call is throwing some data on the stack (so PB can remember it's state when it returns to it).   The stack is limited resource.  But I doubt you'd have the patience to nest enough functions (you'd need hundreds if not 1000's of them)  to cause an overflow as per example #2.  But it is possible !

   The last example is a demonstrating of recursion.   The  example is creating an infinitely recursive loop, which is a obvious no no.  This will certainly cause a stack overflow sooner or later;  More likely sooner !   When the stack overflows the program will stop running and PB will pop an error.