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

975 views

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

In: Technology

6 Answers

Anonymous 0 Comments

If a program calls a subprogram, the program cannot see what the subprogram is doing. Therefore, a return statement is used to get the subprogram to relay a wanted piece of information back to the main program.

Anonymous 0 Comments

You don’t *need* one, and not all languages have one. But they are pretty useful.

Programs are usually divided into functions, mini-programs that perform a specific task. They can be things like GenerateRandomNumber(), InvertMatrix(), or ComputePrice(). They do their thing and come up with a value…return(341) is one way they communicate that value back to whatever requested the function.

Also, a return statement can be used to exit a function early when there is a special case. ComputePrice() might have a statement early on like “if ShoppingCart.size() == 0, return 0” so it doesn’t do unnecessary work when a shopping cart is empty.

Anonymous 0 Comments

Well you don’t always, it depends on the programming language structure.

Most programming languages have the concept of “calling” a function or subroutine. This concept is where you leave from where you are currently, execute some code, and go back to where you were originally.

The catch is: how do you note in the function or subroutine when you are “done” and ready to “go back”. Oh and if it is a function, how do you designate what value to send back to the caller. “Return” fulfills this operation in many programming languages.

That said, some programming languages (like C) only require an explict return if you need to return a value from a function. It is quite common to write a void function that has no explict return statement (functions are delineated by curly braces, the closing brace of the function marks an implicit return). However if a C function returns a value (e.g., an integer) then a return is required in order to determine the value to return, examples include “return 5;” or “return VariableA;”.

Some programming languages always require an explicit return, even if you are NOT returning a value. As a result you might have “return 5” or “return VariableA”, or just “return”.

Does that help?

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.

Anonymous 0 Comments

**Let’s say I ask you to add 5 and 3.** You tell me nothing. You did it in your head, but that’s it. This is an operation *without* a return statement.

**Now let’s say I ask you to add 5 and 3 and tell me the result.** You tell me 8. This is an operation *with* a return statement.

Now, to go into it a little deeper. Suppose I want to do a magic trick. **Let’s say I ask you to think of a number between 1 and 100.** I clearly don’t want you to tell me what you thought of, so I don’t ask for a result. However, you just stand there without saying anything. I can’t continue because I don’t know if you’re done or not.

The previous example teaches us that return statements don’t always have to be the related to the inputs. The correct way would be to **ask you to think of a number between 1 and 100 and tell me when you’re ready.** This is still an operation with a return statement, but instead of telling me the result (37, in case you were wondering), it tells me of the operation was a success or a failure. This way, if I don’t get an answer, I know the operation is still ongoing.

There are other flexible uses of return statements, but I think this should suffice for this ELI5.

Anonymous 0 Comments

Imagine that you and your buddy are working together to build something, but you’re in two different rooms, each doing different things. You get to a point in your work where you need something from your buddy before you can continue. You shout your request over to them, then wait patiently for the answer.

Your buddy hears you shouting, and does what you ask. A “return” statement is them shouting the answer back to you. Without one, your buddy would do what you asked, and then just…be done doing what you asked. You’d have no way to know what their answer was.