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

338 views

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

In: 0

10 Answers

Anonymous 0 Comments

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.

You are viewing 1 out of 10 answers, click here to view all answers.