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

Question

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

Answer

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.

12 Likes

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.

9 Likes

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.

4 Likes

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

2 Likes

Thank you. That makes a lot more sense!

Isn’t it more like

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

No, there is only one variable, x. G(x) is defined as F(x) + 1.

Then, if you decide to evaluate (run) the function G, you can substitute a number. That could be y, but only if you have previously defined y.

# This is the definition
F(x) = x + 1
G(x) = F(x) + 1

# This is the evaluation
y = 5
G(y) = F(y) + 1 = (y + 1) + 1 = 7

Hi all,
I cannot understand at all how can I visualize the data.
I mean, in previous exercises we can see how to print the info stored but now, I cannot find the way.


def one_good_turn(n):
return n + 1

def deserves_another(n):
sad = one_good_turn(n) + 2
return sad
print sad

one_good_turn(2)
deserves_another(3)


Not sure if it’s correct or not how I wrote but it’s like… I have no idea why I can’t see anything on the console. Neither a syntax error or the print request I wrote.

Someone can try to explain me where I fail?