5/19 Funtion calling function


#1

This function only works for me when I use the same parameter for both functions as follows.
def one_good_turn(n):
return n + 1
def deserves_another(n):
return one_good_turn(n) + 2
print one_good_turn(5)
print deserves_another(5)
I would like to know if that’s the correct way of doing it?
I would also like to know why is the use of arguments at the bottom of the editor (and no matter the value of that argument) doesn’t affect the result on the console?
Thanks


#2

Please post a link to the exact exercise this pertains to so everyone is on the same page. Thank you.

Functions Calling Functions

To simplify things, let’s say we have two functions, eh and bee.

def eh():
    return

def bee():
    return

Heck. let’s toss in a third,

def see():
    return

Let’s make it interesting and have see call bee and bee call eh while the main function call will be to see().

>>> def eh():
    return 42

>>> def bee():
    return eh()

>>> def see():
    return bee()

>>> see()
42
>>> 
>>> def bee():
    return "Answer: %d" % eh()

>>> def see():
    return "Question: 'What is the meaning of life?' %s" % bee()

>>> see()
"Question: 'What is the meaning of life?' Answer: 42"
>>> 

see has no variables of its own yet can see the values of the functions it depends upon, bee and in turn, eh. That means see sees 42 and it sees, "Answer: %d" % eh() and is able to put it all in the context of, "Question: 'What is the meaning of life?' %s" % bee(). This is an extensive use of scope chaining or piping of values from one scope to another. Mastering this is as important as mastering boolean expressions.

In my completed exercise there are no print statements.

def one_good_turn(n):
    return n + 1
    
def deserves_another(n):
    return one_good_turn(n) + 2

If n is 1, one_good_turn() returns 2 and deserves_another() returns 4. Focus upon the scope not the argument. n is just a slot through which values are passed.