UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: SpellSword on November 09, 2007, 04:18:19 PM

Title: Call a Function from within a Function?
Post by: SpellSword on November 09, 2007, 04:18:19 PM
Can you have a Function call inside of a Function?

Example:
[pbcode]; Function One Call
FOne()

; Function One
Function FOne()
FTwo()
EndFunction


; Function Two
Function FTwo()
EndFunction
[/pbcode]
Title: Re: Call a Function from within a Function?
Post by: kevin on November 10, 2007, 05:45:30 PM
QuoteCan you have a Function call inside of a Function?

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


[pbcode]

; 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

[/pbcode]

Title: Are there any rule sets or limits for Function Calls in Functions?
Post by: SpellSword on November 14, 2007, 12:26:44 AM
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! (http://www.underwaredesign.com/forums/Themes/default/images/post/cheesy.gif)
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
[pbcode]One()

Function One()
One()
EndFunction[/pbcode]

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!  :)



Title: Re: Call a Function from within a Function?
Post by: nrasool on November 14, 2007, 01:51:39 AM
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
Title: Re: Call a Function from within a Function?
Post by: kevin on November 14, 2007, 05:55:43 AM
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.