In the example, how does fun_one know what m is?



In the example, how does fun_one know what m is?


In the example, how does fun_one know what m is?
i. Since we’re using fun_one() inside of fun_two(), and fun_two() accepts m as a parameter, we
provide that value, whatever it may be, to fun_one() to use as its own parameter – n. Step by step,
what happens is:
1. We use fun_two() and provide some argument for m, like this: fun_two(10).
2. Inside, it makes a call to fun_one() and passes m (which we provided as 10) because it accepts
one argument.
3. fun_one() takes 10 as its n value and multiplies it by 5 and returns the resulting value – 50.
4. Back in fun_two() we now have 50 + 2, so the function returns 52, and that’s it!
Any time you’re unsure of how nested functions are working, it can be helpful to go through step by step like this, even writing it out on paper.

FAQ: Learn Python: Functions - Calling Functions

What is a little confusing to me is that a parameter can be changed from (n) to (m) and that fun_one would accept that even though its orginal paramter was (n). I would assume just from looking at this, that it would return an error saying that there was no argument assigned to (n) in the first place.


When would one function calling another be useful?


It isn’t being changed from M to N, per se.

It’s more like this: fun_two will take the argument M. When fun_two calls fun_one in its own body, it will place its own argument M into a call for argument one.

So it’s like:

  1. I’m fun_two and I got a call with argument (m), I will take it, but now I have to call fun_one, and I will use (m) argument to give fun_one when it asks me for (n).
  2. fun_one won’t take (m), it will take the argument it was given by fun_two; when I, fun_two call, on fun one, I will give it my own argument, because I’m holding a value for (m), but I will tell fun_one it is (n), because that’s the only argument fun_one will take.

Hopefully this explains it a bit more.


If you learned functions in math then this will help you. If not, then learn them because math is fun. Think of it like this:

F(x) = x + 1
G(x) = F(x) + 2

So what is G(1)?

G(1) = F(1) + 2
F(1) = 1 + 1 = 2
G(1) = 2 + 2 = 4

So then onto this problem.

F(x) = x * 5
G(x) = F(x) + 7

What is G(1)?

G(1) = F(1) + 7
F(1) = 1 * 5 = 5
G(1) = 5 + 7 = 12