Programming Search⌘K

Conceptintro

Return value

Also called return statement, result, function result, early return, implicit return.

The return value is the result of a call. A return statement ends the function and gives the value to the caller, and some languages also return the value of the last expression of the body.

Applies to
core-languages

Idea

A call gives back one value to the place of the call. This value is the return value, or the result, of the call. The function chooses it, usually with a return statement, and the caller uses it as the value of the call expression.

def absolute(n):
    if n < 0:
        return -n
    return n


print(absolute(-7))
print(absolute(5) + 1)
Output
7
6

The first call takes the first return, and the second call takes the second one. In each case the call expression absolute(...) has the value that the return statement gave.

How it works

  1. The return statement. The body runs until it reaches a return statement. Java says that "A return statement returns control to the invoker of a method"1, and that "In the case of a return statement with value Expression, the value of the Expression becomes the value of the invocation"1.
  2. The value. The function evaluates the expression after return, then puts the value where the caller can get it. Rust describes this: "Evaluating a return expression moves its argument into the designated output location for the current function call, destroys the current function activation frame, and transfers control to the caller frame."2
  3. The end of the body. A body can also end without a return. Then the language decides the result. Python substitutes None3, JavaScript gives undefined4, and Rust takes the value of the last expression5. In C, C++, Java and C#, a function with a result type must return a value. Procedure and subroutine describes functions with no result.
  4. The caller. The call expression gets the value. The caller can store it, pass it to another call or ignore it.

A return statement also stops the body. MDN says: "When a return statement is used in a function body, the execution of the function is stopped."4 This lets a function return early, for example when an argument is not valid.

In languages

LanguageVerdictNote
PythonNative
C++Native
JavaNative
CNative
C#Native
JavaScriptNative
TypeScriptNative
GoNative
RustNative
KotlinNativeKotlin has a return statement, and a function with an expression body returns that expression. No example file exists because kotlinc is not installed.
SQLNativeA user-defined SQL function gives its result with RETURN. Each database has its own languages for the body. No example file exists.
BashIdiom
PowerShellNativeA PowerShell function returns all the values that it writes to the output, and return can end the function early. No example file exists because pwsh is not installed.
HTMLDoes not applyHTML is a markup language and has no functions. Scripts in a page use JavaScript.
CSSDoes not applyCSS cannot define functions in a standard module, so it has no return values.
Regular expressionsDoes not applyA regular expression has no functions and no return values.
  • Python. "If an expression list is present, it is evaluated, else None is substituted. return leaves the current function call with the expression list (or None) as return value."3 return a, b returns one tuple.
  • JavaScript. return expression; gives back the value. For the expression, MDN says: "If omitted, undefined is returned."4
  • TypeScript. The return type is written after the parameter list. If the code does not write it, the compiler infers it: "The type was inferred - chosen automatically - by TypeScript."6
  • Java. A method with a result type must return a value on each path. "If a method is declared to have a return type" and its body can end without a return, "a compile-time error occurs"7.
  • C#. "If the return type (the type listed before the method name) is not void, the method can return the value by using the return statement."8 "The return keyword also stops the execution of the method."8
  • C. return expression; "Terminates the current function"9 and gives the value. The language does not force a return on each path. "Reaching the end of any other value-returning function is undefined behavior if the result of the function is used in an expression"9.
  • C++. C++ has the same rule as C, but stricter: "Flowing off the end of a value-returning function, except the main function and specific coroutines" and without a return statement "is undefined behavior"10.
  • Go. The Go specification says that a return statement in a function F "terminates the execution of F, and optionally provides one or more result values"11. A Go function can return several values, and the result parameters can have names.
  • Rust. Rust is based on expressions. "You can return early from a function by using the return keyword and specifying a value, but most functions return the last expression implicitly."5 "If you add a semicolon to the end of an expression, you turn it into a statement, and it will then not return a value."5
  • Bash. return n gives back only an exit status. It means "Stop executing a shell function or sourced file and return the value n to its caller."12 A function gives back text by printing it, and the caller captures the text with $( ).
PythonGitHub
def absolute(n):
    if n < 0:
        return -n
    return n


print(absolute(-7))
print(absolute(5) + 1)
Output
7
6
  • Verdict: native. return ends the function and gives the value. A function with no return gives back None.
#include <iostream>

int absolute(int n) {
    if (n < 0) {
        return -n;
    }
    return n;
}

int main() {
    std::cout << absolute(-7) << "\n";
    std::cout << absolute(5) + 1 << "\n";
}
Output
7
6
  • Verdict: native. return ends the function and gives the value, as in C.
JavaGitHub
public class Main {
    static int absolute(int n) {
        if (n < 0) {
            return -n;
        }
        return n;
    }

    public static void main(String[] args) {
        System.out.println(absolute(-7));
        System.out.println(absolute(5) + 1);
    }
}
Output
7
6
  • Verdict: native. The result type comes before the method name, and the compiler checks that each path ends with a return.
#include <stdio.h>

static int absolute(int n) {
    if (n < 0) {
        return -n;
    }
    return n;
}

int main(void) {
    printf("%d\n", absolute(-7));
    printf("%d\n", absolute(5) + 1);
    return 0;
}
Output
7
6
  • Verdict: native. return ends the function and gives the value. The compiler does not force a return on each path.
using System;

class Program
{
    static int Absolute(int n)
    {
        if (n < 0)
        {
            return -n;
        }
        return n;
    }

    static void Main()
    {
        Console.WriteLine(Absolute(-7));
        Console.WriteLine(Absolute(5) + 1);
    }
}
Output
7
6
  • Verdict: native. The return type comes before the method name, and the compiler checks that each path ends with a return.
JavaScriptGitHub
function absolute(n) {
  if (n < 0) {
    return -n;
  }
  return n;
}

console.log(absolute(-7));
console.log(absolute(5) + 1);
Output
7
6
  • Verdict: native. return ends the function and gives the value. A function with no return gives back undefined.
TypeScriptGitHub
function absolute(n: number): number {
  if (n < 0) {
    return -n;
  }
  return n;
}

console.log(absolute(-7));
console.log(absolute(5) + 1);
Output
7
6
  • Verdict: native. The return type follows the parameter list. The compiler checks that each return gives a value of that type.
package main

import "fmt"

func absolute(n int) int {
	if n < 0 {
		return -n
	}
	return n
}

func main() {
	fmt.Println(absolute(-7))
	fmt.Println(absolute(5) + 1)
}
Output
7
6
  • Verdict: native. The result type follows the parameter list, and the compiler checks that the body ends in a return. A Go function can also return several values.
RustGitHub
fn absolute(n: i32) -> i32 {
    if n < 0 {
        -n
    } else {
        n
    }
}

fn main() {
    println!("{}", absolute(-7));
    println!("{}", absolute(5) + 1);
}
Output
7
6
  • Verdict: native. The if is an expression, and the function returns the value of the last expression of its body, so it needs no return.
BashGitHub
absolute() {
  if (( $1 < 0 )); then
    echo $(( 0 - $1 ))
    return
  fi
  echo "$1"
}

absolute -7
echo $(( $(absolute 5) + 1 ))
Output
7
6
  • Verdict: idiom. return gives only an exit status, so the function prints the value and the caller captures it with $( ).
  • Implicit returns. In Rust, the last expression of a block is its value, so a function often has no return statement5. Arrow functions in JavaScript with an expression body return that expression.
  • Early returns and guard clauses. A function can test for a bad case at the top and return at once. The rest of the body then handles only the normal case.
  • Several results. Go returns several values at once11. Python returns a tuple. Other languages return a record or an object. See Use a tuple.
  • No result. A function that gives back nothing is a procedure. Its result type is void in C, Java and C#, or the unit type in Rust. See Procedure and subroutine.
  • Results through parameters. C and C# can give results through pointer, ref or out parameters. See Parameter passing.
  • Errors instead of results. A function that cannot compute a result can throw an exception or return an error value, such as an error in Go or a Result in Rust.

Pitfalls

A line break after return in JavaScript

JavaScript inserts a semicolon at the end of a line after return. MDN gives the case return a + b with a line break after return, and says: "This makes the function return undefined and the a + b expression is never evaluated."4

This code runs but gives a wrong answer on purpose.

function total(a, b) {
  return
    a + b;
}

console.log(total(2, 3));
Output
undefined

Put the start of the expression on the same line as return. To split a long expression, put it in parentheses that start on the return line.

A return inside a loop that ends too early

A return ends the whole function, not only the loop. A return in both branches of a test in a loop ends the function in the first pass:

This code runs but gives a wrong answer on purpose.

def has_negative(numbers):
    for n in numbers:
        if n < 0:
            return True
        else:
            return False


print(has_negative([4, -2, 7]))
Output
False

return "leaves the current function call"3, so the function looked only at 4. Return True in the loop, and return False after the loop, when all the numbers are checked.

A return inside a callback

A return statement stops the function that contains it4. Inside a callback, such as the arrow function that forEach calls, return ends only the callback, not the outer function:

This code runs but gives a wrong answer on purpose.

function firstNegative(numbers) {
  numbers.forEach((n) => {
    if (n < 0) {
      return n;
    }
  });
  return null;
}

console.log(firstNegative([4, -2, 7]));
Output
null

The return n gave -2 back to forEach, which ignored it, and then the outer function returned null. Use a for...of loop, so that return belongs to the outer function, or use numbers.find((n) => n < 0).

Cost

A return costs little: the function puts the value in a register or in a place that the caller gave, and jumps back. A large value, such as a big structure, can cost a copy. Rust moves the value into "the designated output location for the current function call"2.

History

ALGOL 60 had no return statement. A procedure that gave a value assigned the value to its own name: "there must, within the procedure declaration body, occur one or more explicit assignment statements with the procedure identifier in a left part"13. Pascal kept this rule. "The function-block shall contain at least one assignment-statement such that the function-identifier of the assignment-statement is associated with the block"14, and a call "shall yield the value of the result of the activation upon completion of the algorithm of the activation"14.

C used a return statement with an expression, which ends the function and gives the value at the same time9. Java, C#, JavaScript, Python and Go use the same statement. Rust keeps return but makes the last expression of the body the result5.

References

  1. Blocks, Statements, and Patterns, The Java Language Specification, Java SE 25 Edition, 14.17 The return Statement
  2. Return expressions, The Rust Reference, expr.return.intro; expr.return.behavior
  3. Simple statements, The Python Language Reference, 7.6 The return statement
  4. return, JavaScript reference, MDN Web Docs, Syntax; Description; Automatic semicolon insertion
  5. Functions, The Rust Programming Language, Functions with Return Values
  6. More on Functions, The TypeScript Handbook, Generic Functions, Inference
  7. Classes, The Java Language Specification, Java SE 25 Edition, 8.4.7 Method Body
  8. Methods, C# Programming Guide, Return values
  9. return statement, C reference, cppreference.com, Explanation
  10. return statement, C++ reference, cppreference.com, Notes
  11. The Go Programming Language Specification, Return statements
  12. Bourne Shell Builtins, Bash Reference Manual, return
  13. Revised Report on the Algorithmic Language Algol 60, 5.4.4 Values of function designators
  14. ISO 7185 Pascal, 6.6.2 Function-declarations; 6.7.3 Function-designators

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/GD94GQ/text.yaml