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

995 views

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

In: Technology

6 Answers

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.

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