DEV Community

Valentin Silvestre
Valentin Silvestre

Posted on

Explain Return vs Passing Reference Like I'm Five

Top comments (2)

Collapse
 
tgc profile image
tgc

Hope this helps although it`s not %100 accurate

You have a 12 years old kid Johnny who likes chocolate cheese cake very much and if he does not eat everyday he gets very sad.

So everyday you go to your favorite bakery. This bakery has 2 options for you.

(1)If you order a standard chocolate cheese cake and they wrap it up for you it takes 16 minutes for you to leave the shop.15 minutes goes for baking and 1 minute for them to fold up the carrier box and putting the freshly cooked cake in it.Costs you $5 dollars

(2)If you bring your own carrier box which is already folded it takes just 45 seconds to package and costs $4.95.

Using (2 - Pass by reference)Johnny can take the first slice 15 seconds before when compared to (1 - Return value). And it`s less expensive (less cpu clock cycles)

It does not dramatically improve Johnny `s life and your budget but 15 seconds is 15 seconds and 5 cents is 5 cents.

If you are working on simple objects, the performance would be similar in both cases.

As he is a grooving kid next year Johnny starts to ask for a chocolate cake with a bagel and a lemonade. Seems like he is hungry for carbohydrates but anyway , you know kids:)

if you choose (1) you need to wait 3 minutes for packaging because now there are more items to be separately packed and it costs you $6.70.

However with the option (2) if you take your own lemonade cup , cake & bagel carrier boxes you can leave there in 1 minute by paying $5.50.

This time choosing (2) might have significant effect given that this is your routine and you are rushing to catch a bus.

If the object is so large and complex, returning it would need a copy, and it could be slower than having it as a referenced parameter, but it would spend less memory.

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.