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 + 7
, so the function return
s 57, 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.
11 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:
- 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).
- 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.
10 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
13 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
1 Like
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?
52? The answer should be 57…
since Fun_one(10) would be 50, and Fun_Two(10) would be 50 + 7.
@data5301176295, thanks for pointing out that the values used did not match the example from the lesson. The mistake has been corrected. 
1 Like
Hello,
The code should be modified like this:
def one_good_turn(n):
return n + 1
def deserves_another(n):
return one_good_turn(n) + 2 #here it always add 2 (SOLUTION)
#code below just visualize result
result = deserves_another(4)
print result
best
1 Like
Hello, @vonsathonyx.
Welcome to the forums!
See my comments to your code below.
There are a couple of ways you could print your output. Either print sad
before you return sad
inside the function, or print the returned value of the function like so:
def one_good_turn(n):
return n + 1
def deserves_another(n):
sad = one_good_turn(n) + 2
return sad
print one_good_turn(2)
print deserves_another(3)
P.S. When pasting your code into a post, please follow these guidelines: How do I format code in my posts? Then your code will appear as it does in this post and in your original post which I edited.
It is confusing because in this example not enough data is presented to the beginner.
Here, the same example but with enough information for us newbie coders to breakdown the problem and figure out how the argument for fun_two(m) became also the argument for fun_one by calling its value the equivalent of (m), etc.
def fun_one(n):
return n * 5
def fun_two(m):
return fun_one(m) + 7
fun_two(10)
result = 57
Just a formula or number relationship
When would we want to call a function with another function? As a coding newbie, I would’ve loved to see a real-world example of this (like the example used for calculating the tip and tax for a bill—that was great!).
I guess a good example of a function calling another would be a coffee maker simulator, since you have to pick the coffee, transfer the grain to someone who would grind the grains, who would return the result to some other person/robot who would put that powder to receive boiling water, etc.