What is "return" and functions calling functions?


I’m currently on the Function lesson on Python. I kinda understand them (still learning but I’ll get there) but the thing I don’t understand is the “return” keyword that shows up in many examples. It isn’t explained anywhere and it’s really confusing me. I read a topic old 5 years explaining what it is, but I still don’t get it (this is the link to it if anyone wants to see: https://www.codecademy.com/en/forum_questions/51c0e35d7c82caace80008b1#answer-51c1a3059c4e9dd0fb00a7a0 ). If anyone has a better explanation, please leave a reply 'cause I’m still stuck on the same lesson for hours trying to understand.

Now for the functions calling functions part - there is also a lesson about that and it gives one example and a task which I don’t understand.

def fun_one(n):
  return n * 5

def fun_two(m):
  return fun_one(m) + 7
def one_good_turn(n):
  return n + 1
def deserves_another(n):
  return n + 2

Now this confused me a lot. In the first example they first defined a function “fun_one” and gave it a parameter with a name “n”. Then in the function “fun_two” it is being returned and has a parameter named “m”. I basicly don’t know what any of it means or what it does because if I run it, nothing visibly happens.

Then in the second example we have 2 functions.One returns n + 1, the other returns n + 2. In the task it says:
"Let’s look at the two functions in the editor: one_good_turn (which adds 1 to the number it takes in as an argument) and deserves_another (which adds 2).

Change the body of deserves_another so that it always adds 2 to the output of one_good_turn."
So, does that basicly mean it will take an input and then add 1 to it and make it so n = n + 1 then other function takes that n and adds 2 to it so it is like n = n + 2?


Hey @ledaraa

I think the best way to explain is with an analogy.

Let’s say we’re at work in an office, and we need to ask our colleague Dave to put together the quarterly report on how sales were for our company’s flagship product, “Product X”. We might ask Dave to write other reports, too, so the process of getting Dave to write the report is kind of like a function - it’s a repeatable process we can use whenever we need to.

So, we could have something like this:

def make_dave_write_a_report(product, sales_figures, due_date):
  # Dave does the work here... 
  report = dave is doing the work
  return report

In this analogy, the content of the function is Dave writing up the report. You can see that we’ve given him the product we want the report on, as well as the sales figures and the date the report needs to be done by. So, in our “Product X” example, we’d call the function by writing make_dave_write_a_report("Product X",[list,of,sales,figures,here],"next tuesday") for example.

The return statement inside of the function is where Dave hands you back the completed report (hence, return report). Similarly, if you declare any variables inside of a function and you need to get the value of those variables back into the main body of your program, you need to return them from the function.

To use the examples you posted, we can do this:

def fun_one(n):
  return n * 5

>>> fun_time = fun_one(5)
>>> print fun_time

In this example, we’ve got the function fun_one() and we’re calling it with an argument of 5. fun_one returns n * 5, which is 25 in this case, and we assign that value to the variable fun_time so we can use it.

return doesn’t visibly do anything, it’s the mechanism by which your function hands data over to another part of your program. If you just call the function, like fun_one(5), then Python will do the work but the output from that function call doesn’t go anywhere. If you do fun = fun_one(5), then the output from the fun_one function gets assigned to the fun variable, and then you could do print fun and see 25 on the console.

Does that help at all?


Here’s another analogy… Think of a server in a restaurant. They take your order, that is the argument that gets passed to the kitchen. The kitchen takes that order and prepares it then notifies the server it is ready for pickup. The server returns from the kitchen with your order.

The kitchen is a different scope than the front of house. The order is passed to that scope, processed and returned to the scope of the dining room as a finished meal.

In program lingo, you in the dining room are the caller, the server is a function who takes your order, then calls the kitchen, another function, with the server being the caller. The kitchen returns to the server, the server returns to you.


That helped a lot. Had to read it a couple of times to fully understand but I get it now. Thank you both so much! :blush:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.