# ELI5. What does “return” do in programming? I read about it a lot and still dont understand the purpose.

274 views

ELI5. What does “return” do in programming? I read about it a lot and still dont understand the purpose.

In: 0

You can create small pieces of code that we call “function”. Those function take a few input that we call “arguments” or “parameters”. They execute whenever they are meant to do… and usually return an output we call “result”.

An example of function is “distance”. It takes two coordinates as input and compute the distance between those coordinates and returns said distance.

So that does the return do in programming? It terminate the function it’s in, and takes an optional argument which is the return value of the function.

Computers do lots of calculations. The vast majority of the time, you don’t want to actually know the answer to a calculation. You only want to know a certain thing when a certain other thing happens. You use “return” to output the answer.

Let’s say you want to look for Pythagorean triples, three numbers a, b and c such that a²+b²=c², where a, b and c are all positive whole numbers. You get the computer to run through different options for a and b. It will square them and add them together. Then, it takes the square root of that and checks if it’s a whole number. If it is, then you’ve found one and you want to know about it. But the rest of the time, you don’t care!

You’d maybe set up the code to try 1²+2²=1+4=5. It tries √5, but it’s not a whole number.

Okay, now it tries 1²+3²=1+9=10. But √10 doesn’t work either.

Then 2²+3²=4+9=13, but √13 ×

1²+4² fails.

2²+4⁴ fails.

But 3²+4²=9+16=25 and √25=5. So the computer has found a solution and *returns* 3,4,5.

“return” essentially means that the function is done, and provides a way of giving an output. Usually a function is “called” by another function which needs its results, so the return keyword is saying we are done and ready to go return to the calling function.

The reason why we use a special keyword has to do with how functions are actually called. Each time a function is called, it’s parameters, local variables, etc are saved to RAM in a block called a stack frame. But during the computation of this function, you may need to call another function. The parameters and local variables are saved into another stack frame next to it. The return keyword initiates the procedure of deleting the current stack frame, and moving back to the older one.

Imagine you had a smart little robot, Eff, who did nothing but tell you how long it would take to get to a destination in your helicopter, given the two starting points, but it took him an hour to figure it out. So you say “Eff, how long will it take me to fly from Albany to Albuquerque?”, and Eff goes away, and *returns* an hour later with “7.4 hours, boss!”. IOW, Eff(Albany, Albuquerque) returns 7.4.

In programming, f(x) can be any function you want it to be. In this case, it returned a number of hours, but it could return a name e.g. TOPSALES(Jan) could return ‘Joe Smith’ as the best salesman. The term ‘return’ comes because you give the function the input data, and it ‘goes away’, figures out the answer, and then ‘returns’ with the answer. Meanwhile, your main program is waiting, and can’t move on until the function returns its answer. Thus, at that point, the function both returns with an answer, and *hands control back to the main program*.

Currently, this isn’t a big deal, as most functions execute almost instantly but in the old days, you could wait five, ten, or more minutes waiting for a function call to ‘return’.

In most cases, it ends the current function and resolves in either a value or void/undefined depending on function type. Of course it depends on language and this isn’t necessarily true for all languages, but it covers most.

A method can either do a function by the orders given from the caller and end or it can do a function that change a variable that can be returned to the caller.

Method not returning:
“Open a box of eggs and smash them”….Done. The End.

Method returning:
“Open a box of eggs and give one to me”…Here I’m returning one egg.

Clarification: _Which_ programming language? `return` does different things in different languages. The answers you’re getting are very specific to languages like C and it’s derivatives, but this is NOT global.

When you have a bit of code that will be run over and over again with different inputs each time, you write a function. We need a start and an end to the function. Multiple ends are ok, but we always need to make sure we end it. If we don’t don’t the function, the code will just keep going, reading whatever is next after the function, thinking it’s still in that function, and thats not ok. It could read anything, maybe even stuff you haven’t written because it’s not going through the code, but rather it goes through the memory of the computer and who knows what bits it will interpret as commands, what it will overwrite or what mischief it gets up to.

Let’s write a function called divide

Divide(a, b)

If(b==0){

Return null

}

C = a/b

Return c

This is properly written code. If I instead left out that “return null” the function would try to do a/0, which it can’t do.

Now lets write a function called multiply.

Multiply(a, b)

c = a * b

Return c

Here, if we left out return, the code would just keep running on and on through whatever memory address follow multiply. Let’s say our function divide is kept there, then instead of returning a * b, it would return a/b

As with all things related to programming, it depends on the language. It could conceivably do anything.

However, in most languages, including C, Java, Basic, and Python, the `return` statement causes the program to exit the current function or subroutine and optionally report a value.

E.g. if I wrote a function called `add_one(x)` as follows:

y = x + 1
return y

Then when I call the function, e.g. `add_one(10)` it outputs the value of `y`, in this case 11.

In principle, a simple computer can only be occupied with one task at any given time. These tasks are called “threads”. A computer can have a lot of threads all scheduled at the same time, but if you took a snapshot of the computer’s brain, only one thread will be there actively being worked on while the others are waiting patiently for their turn.**

The program that the computer is working on, you can imagine it holding a little runner’s baton. Like the ones relay race runners will pass to each other when they race. The piece of code currently holding the baton is the one the computer is currently thinking about. All others are on hold.

When one piece of code holding the baton needs a simple task done, it can either do that task itself, or it can hand its baton to a smaller chunk of code that will do that task (and only that task) for it. Like… say you wanted some donuts. You *could* just make your own yourself. But there might be a dedicated Donut Guy^(tm) who can do it for you. You can walk up to him, tell him, “Run to the store and get me some donuts”, and hand him your baton, and he’ll go take care of it. As soon as he’s done with his task, he’ll come back and *return* the baton to you. That’s essentially what `return` in a programming language means–it’s a command that tells the current running piece of code, “Your job is done, now go *return* the baton to the one who gave it to you”.

`return` has a second purpose that may or may not be utilized. It is able to take some piece of data that the other piece of code generated and hand it off to the first piece of code that gave them the baton. This could be, for a couple examples:

* a piece of data the secondary piece of code was supposed to fetch (like, in our example, Donut Guy^(tm) went to fetch donuts, so he should give you those donuts in addition to the baton when he comes back)
* a piece of data that needed to be calculated (say, some who can take two numbers when you give them the baton and come back with the two numbers added together)
* a status report of whether the task was successful or not

But it could just as well hand over absolutely nothing at all if nothing needs to be handed over.

In many languages, `return` doesn’t even need to be explicitly used sometimes. If the computer running a subtask gets to the end of the instructions for that task, it will just return automatically. So actually writing `return` in the code is only needed if you want to end the task prematurely, or if you want it to also yeet out a value when it returns.

**There’s some nuance to that, of course… modern home computers have multiple *cores*, which means they essentially have multiple tiny computers within them that can all work on independent tasks, allowing the computer to truly multitask. In addition to that, computers can do a little work on one task, pause it, switch to another one, do a little work on that one, pause it, etc. so quickly that it can look like it’s multitasking when it really isn’t. But to keep things simpler for this explanation, we can just just assume we have a computer with one core, with one job to do.