Hubbry Logo
search
logo

Return statement

logo
Community Hub0 Subscribers
Write something...
Be the first to start a discussion here.
Be the first to start a discussion here.
See all
Return statement

In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is saved by the calling routine, today usually on the process's call stack or in a register. Return statements in many programming languages allow a function to specify a return value to be passed back to the code that called the function.

In C and C++, return exp; (where exp is an expression) is a statement that tells a function to return execution of the program to the calling function, and report the value of exp. If a function has the return type void, the return statement can be used without a value, in which case the program just breaks out of the current function and returns to the calling one. Similar syntax is used in other languages including Modula-2 and Python.

In Pascal there is no return statement. Functions or procedures automatically return when reaching their last statement. The return value from a function is provided within the function by making an assignment to an identifier with the same name as the function. However, some versions of Pascal provide a special function Exit(exp); that can be used to return a value immediately from a function, or, without parameters, to return immediately from a procedure.

Like Pascal, FORTRAN II, Fortran 66, Fortran 77, and later versions of Fortran specify return values by an assignment to the function name, but also have a return statement; that statement does not specify a return value and, for a function, causes the value assigned to the function name to be returned.

In some other languages a user defined output parameter is used instead of the function identifier.

Oberon (Oberon-07) has a return clause instead of a return statement. The return clause is placed after the last statement of the procedure body.

Some expression-oriented programming language, such as Lisp, Perl and Ruby, allow the programmer to omit an explicit return statement, specifying instead that the last evaluated expression is the return value of the subroutine. In other cases a Null value is returned if there is no explicit return statement: in Python, the value None is returned when the return statement is omitted, while in JavaScript the value undefined is returned.

In Windows PowerShell all evaluated expressions which are not captured (e.g., assigned to a variable, cast to void or piped to $null) are returned from the subroutine as elements in an array, or as a single object in the case that only one object has not been captured.

See all
User Avatar
No comments yet.