Concepts / Return Values

Return Values

The return statement is used to return from a function i.e. break out of the function. We can optionally return a value from the function as well.

  • Programming

What Return Does

When you call a function, the program jumps into that function's code, executes its statements, and then needs to know when to stop and go back to where it was called. The return statement is how a function tells the program: I am done, exit me now, and here is what I computed. Without a return statement, a function runs until it reaches the end of its code block. With a return statement, the function stops immediately, no matter where it appears in the code.

The return statement serves two purposes: it breaks out of the function (stops execution), and it optionally sends a value back to the code that called the function.

How Execution Flows When Return Happens

To understand return values deeply, you need to see what happens inside a function when a return statement executes. The function does not continue to the next line. Instead, control immediately jumps back to the line that called the function, carrying the returned value with it.

callexecutetrueexit functionCaller: result =maximum(3, 5)Enter maximumfunctionif a > breturn aCaller receivesvalue, continues
What happens inside a function when return is called? Where does control go next?

Notice that once return is reached, any code after it in the function is never executed. The function stops completely and hands control back to the caller. This is what we mean by break out of the function.

Returning a Value to the Caller

A return statement can include a value. That value is computed inside the function and then delivered back to the code that called the function. The caller can then use that value—store it in a variable, pass it to another function, print it, or use it in a calculation.

1. Call: maximum(3, 5)2. Compute: 5 is greater3. Return: 54. Receive 5, assign to resultCaller Codemaximum Function
How does a value computed inside a function get delivered to the code that called it?

Finding the Maximum of Two Numbers

Write a function that takes two numbers and returns the larger one.

Define the function: Create a function called maximum that accepts two parameters, a and b.

Compare the values: Use an if statement to check if a is greater than b.

Return the larger value: If a > b, return a. Otherwise, return b. This is the value that goes back to the caller.

Call the function and use the result: When you call maximum(3, 5), the function computes and returns 5. You can store this in a variable like result = maximum(3, 5), and result now holds 5.

The function returns the maximum value, and the caller receives it and can use it immediately.

Return Without a Value

A return statement does not have to include a value. You can write return by itself, with nothing after it. When you do, the function still exits immediately, but it returns a special value called None. None is Python's way of representing nothingness or the absence of a value.

A return statement without a value is equivalent to return None. If a function reaches its end without an explicit return statement, it also returns None automatically.

This is useful when a function's job is to perform an action (like printing, modifying data, or writing to a file) rather than compute and deliver a value. The function still returns, but the caller knows not to expect a meaningful result.

exampleexampleReturns a ValueReturns Nonereturn maximumCaller receives 5return (no value)Caller receives None
What's the difference between a function that returns nothing and one that returns a value?

Common Mistakes with Return Statements

  • Forgetting that return stops the function immediately

    Once return executes, the function exits. Any lines after return in that function are never reached.

    Fix: Place return at the end of the function, or structure your logic so that return is called only when you are truly done with the function.

  • Confusing return with print

    print sends output to the screen but does not send a value back to the caller. The function still returns None unless you explicitly return a value.

    Fix: Use return value to send a value back to the caller. Use print only if you want to display output on the screen.

  • Returning a value but not using it

    The function computes and returns 5, but if you do not capture it in a variable or use it, it is lost.

    Fix: Store the result in a variable: result = maximum(3, 5), or use it immediately: print(maximum(3, 5)).

  • Not understanding that return None is implicit

    Every function returns something. If there is no explicit return, Python returns None automatically.

    Fix: Be aware that functions without a return statement return None. If you want a value returned, add an explicit return statement.

When to Use Return Values

Use return when your function computes a value that the caller needs. For example, a function that calculates the maximum of two numbers, computes a sum, or validates input should return the result. This makes your function reusable and composable—the caller can use the returned value in further calculations or decisions.

Use return without a value (or let the function end naturally, returning None) when your function performs an action but does not need to deliver a result. For example, a function that prints a greeting, writes data to a file, or updates a data structure might not return a meaningful value.

Practice

EASY

Write a function called is_positive that takes a single number as a parameter. The function should return True if the number is greater than zero, and False otherwise. Then call your function with the values 5, -3, and 0, and print the results. What does your function return for each input?

Hints
  • Use an if statement to check if the number is greater than zero.
  • Return True or False based on the condition.
  • Call the function three times with different values and print each result.

Summary

  1. The return statement exits a function immediately and optionally sends a value back to the caller.
  2. When a function executes return value, that value is delivered to the code that called the function and can be stored in a variable or used directly.
  3. A return statement without a value (or no return statement at all) returns None, Python's representation of nothingness.
  4. Use return when your function computes a value the caller needs; use return without a value when the function performs an action but does not need to deliver a result.
  5. Code after a return statement in a function is never executed—return stops the function completely.

Key Takeaways

  • The return statement breaks out of a function and optionally sends a computed value back to the caller.
  • Returned values are immediately available to the code that called the function and can be stored or used in further operations.
  • A return statement without a value returns None, which is useful for functions that perform actions rather than compute results.
  • Any code after a return statement in a function is unreachable and will never execute.
  • Design functions to return values when they compute something useful, making them reusable and composable.