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

991 views

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

In: Technology

6 Answers

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?

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