Explain Return Like I'm Five

Did you find this post useful? Show some love!

So, we can think of a function as a box that does things, right? We put things into the box and the box acts upon them.

For example, imagine a box called "plus". The plus box will add together whatever things we happen to put into it. Like this:

1 ────> β”‚ ───┐            ┃
        ┃    1  +  2      ┃
2 ────> β”‚ β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      ┃

Right now, we've used "plus" to add 1 and 2. We have 3, but it's just sitting in our box. We want 3 to be able to get out of the box:

1 ────> β”‚ ───┐            ┃
        ┃    1  +  2 ────>  3
2 ────> β”‚ β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      ┃

The return keyword is what allows us to get the 3 out of the box. Now, we give our box 1 and 2, and get 3 in return.

Classic DEV Post from Apr 26

What do you think of the new Go logo?

I got a hilarious message from my friend about Go's new look & logo announc...

Follow @jess to see more of their posts in your feed.
Valentin Silvestre
Member since Jul 7, 2017
More from @vasilvestre
Explain what really happening when you put final in a class Like I'm Five
Explain Return vs Passing Reference Like I'm Five
Trending on dev.to
Code camp or self-learning
#discuss #productivity
What are good ways to learn software architecture and systems design?
#advice #discuss #architecture #reading
What newsletters are you subscribed to?
#discuss #learning #newsletters
Memoization in a Nutshell
#explainlikeimfive #beginners #javascript
If you could not code anymore, what would you do?
Note taking as a developer
#discuss #notes
They Watched Us With Webcams And Rewrote Our Code!
#career #culture #databases #story
What's in your bookmarks bar?
#tools #productivity #discuss