Explain in layman’s term why we need to use ‘return’ statement in programming ?

994 views

Explain in layman’s term why we need to use ‘return’ statement in programming ?

In: Technology

6 Answers

Anonymous 0 Comments

I am a program called squeezeOranges that needs to create orange juice. To create orange juice, i need to squeeze some oranges. But to do that, i need to actually get some oranges.

So i call a sub-program, collectOranges(int), which collects oranges, and then returns a value (in this case, an int value) which tells me, the squeezeOranges program, how many oranges i will be squeezing to produce juice.

If i just called a collectOranges program without return functionality, then the sub-program would say “Okay cool, im done collecting them, you can start squeezing!”, but i dont know how many i need to squeeze. I cant magically figure out how many oranges were collected, i dont know how many empty bottles i will need, i dont know how many days of squeezing i need to budget etc. And so i need to make sure the collectOranges returns a value that i can use to continue with my program.

Edit: Because apparently ELI5 bots dont like word “Oranges”, im adding a ping to /u/manozonam to make sure he sees it when this is restored.

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