Ex35 Calling a function inside the body of another function


#1

Hi all,

I see in the code at ex35 that functions are called inside the body of other functions (e.g., dead() and start() are called inside the function cthulhu_room()). I have been trying to do that in my own code, but failed. Do you know any requirements for being able to do this magic?


#2

Hy @puskini Welcome to the forum.

Yes, you can call a function inside another function. And that is a fundamental principle of programming:

Look at the following code:

def function_one():
    print("I'm function one")

def function_two():
    print("I'm function two")
    function_one()

function_two()

Here you have two functions that have a print statement. The second one calls the first one inside the function.
On the last line you call the second function. If you now run this little program it will call the second function first and then from inside it the first function. So you will have the following output:

I'm function two
I'm function one

#3

Hi @DidierCH,

Thank you for your response! Your answer helped me solve my problem, and it helped me understand that the function called in the body of another must be first defined (i.e., function_one() defined before function_two() and not function_two() and then function_one().

All the best!