0

When I started off creating games on python or other applications, I was taught to use a while loop, in which the game would continuously register for events and react accordingly.

However, before I was taught this, I applied a different method, which consists of jumping sequentially from one function to the next. I really like this method, because it seems quite straightforward to me, but I was wondering whether there might be some issues with it, since no one seems to be using this.

Here an example:

def funcA(x):
    x = input("Give a True or False value for x: ")
    #We create a menu of possible actions:
    if x:
        funcB():
    else:
        funcC():

def funcB():
    #Some sort of job is completed here:
    #----------
    #And then we return back to the menu:
    funcA(x)

def funcC():
    #Some other job is completed here:
    #----------
    #And then we return back to the menu:
    funcA(x)

My suspicion is that python puts into memory each level within a function, and with this method, running it continuously would end up in an infinite memory requirement.

user3604362
  • 361
  • 1
  • 5
  • 19
  • 3
    Recursive functions are just a fancy loop. And one that will sooner or later result in a stack overflow. – Some programmer dude Dec 14 '17 at 11:41
  • It also introduces a limitation by coupling your functions to `funcA`. What if you wanted to call `funcB` without going back to `funcA`? It's good practice to give each function the least amount of responsibilities required to perform a task. This gives you more flexibility later on if you decide to make a change. – Jessie Dec 14 '17 at 12:01
  • @Someprogrammerdude In this example, many languages would actually optimize the tail recursion. Python, however, would not [Does Python optimize tail recursion?](https://stackoverflow.com/questions/13591970/does-python-optimize-tail-recursion) – Lanting Dec 14 '17 at 12:12
  • Hm. @Someprogrammerdude, that was actually quite a useful link. Now I am wondering though whether there is a way to avoid python to memorize the recursion levels. (So still use the same setup as above, but make python take the functions simply as commands rather than commands within commands etc. ) Either way, this has clarified to me why this is not the 'pythonic' way to program. – user3604362 Dec 14 '17 at 16:18

0 Answers0