DEV Community

Discussion on: Explain Return vs Passing Reference Like I'm Five

Collapse
 
overlordex3 profile image
Exequiel Beker

Okay, let's try (maybe i'm wrong).

Susan, your kinder teacher, gave you a task: Draw a lovely sun, with smiles, colours and stuff. You know how to do it, but you need the paper.
Today, Susan gives you one sheet directly to you. You, fascinated with this cool task, draw a awesome sun, with a radiant smile, sunglasses, and a complex set of colours and shadows. You're such an artist! Once you finished, you return the sheet to your teacher, and she keeps in a box.

But, the following day, she gaves you the same task. There're no problem, you had some new ideas. However, this time she doesn't give you a sheet. Instead of it, she tells you where to take (and where to leave) a sheet of paper.

  • Use the papers that are in that box, the second one in that shelf. -she said.

Astonished, you realized that she gaves you a reference where you can find a sheet (or not. It's just a reference to a box).
You look for that box, grabs the paper, and do your job. This time, is more beautiful than the last week's one, and you -and I, too- are proud of it. You finished, so, you take the draw to the box, and leave it inside.

In the first example, you gave(returned) her(the caller) your draw (the result of your function). So, you returned something.
In the second one, she gave you the location (reference) of the box, where you have to take and leave your work.