Explain Return Like I'm Five

Did you find this post useful? Show some love!
Ben Halpern DEV.TO FOUNDER

Hey there, we see you aren't signed in.

Please consider creating an account on dev.to. It literally takes a few seconds and we'd appreciate the support so much. ❤️

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 Feb 12

I just got a Raspberry Pi 3. What can I do with it?

Just got a Raspberry Pi 3 setup and it's my first one. If you own a Raspberry P...

READ POST
Follow @michael to see more of their posts in your feed.