CS Pathfinder Logo CS Pathfinder

Unit 3 · Functions

Return Statement

The return statement sends a value back from a function to the caller. Understand how to return values, return multiple values, and the critical difference between print() and return.

Introduction

In the previous chapters, you learned how to define functions and pass arguments to them. However, many functions need to produce a result that can be used elsewhere in the program. For example, a function that adds two numbers should not just print the sum—it should return it so you can store it in a variable or use it in further calculations.

The return statement serves this purpose. It sends a value back to the caller and immediately exits the function. Every function that does not have a return statement (or has return with no value) automatically returns None.

Mastering the return statement is essential for writing modular, reusable, and testable Python code.

University Definition

The return statement is used inside a function to send a value back to the caller. It terminates the execution of the function and passes the specified value to the point where the function was called. If no value is specified, the function returns None.

Basic Return Example

def add(a, b):
    result = a + b
    return result

sum_value = add(10, 20)
print("Sum =", sum_value)
Output:
Sum = 30

Step-by-step explanation:

  1. add(10, 20) is called. a = 10, b = 20.
  2. result = 10 + 20, so result = 30.
  3. return result sends 30 back to the caller.
  4. The returned value is stored in sum_value.
  5. print() displays it.

print() vs return — Critical Difference

This is one of the most commonly tested concepts in exams. Many students confuse print() with return.

Using print()

def add(a, b):
    print(a + b)

result = add(10, 20)
print("Result:", result)
30
Result: None

print() only displays on screen. The function returns None because there is no return statement.

Using return

def add(a, b):
    return a + b

result = add(10, 20)
print("Result:", result)
Result: 30

return sends the value back. You can store it in a variable and reuse it.

Returning Multiple Values

Python allows a function to return multiple values using tuple packing:

def calculate(a, b):
    addition = a + b
    subtraction = a - b
    multiplication = a * b
    return addition, subtraction, multiplication

add, sub, mul = calculate(10, 3)
print("Add:", add)
print("Sub:", sub)
print("Mul:", mul)
Output:
Add: 13
Sub: 7
Mul: 30

Internally, return a, b, c creates a tuple (a, b, c) and returns it. The caller can unpack the tuple into separate variables.

Functions Without return (Returns None)

If a function does not have a return statement, or has return with no value, Python automatically returns None:

def greet(name):
    print("Hello,", name)

result = greet("Alice")
print(type(result))
print(result)
Output:
Hello, Alice
<class 'NoneType'>
None

Important: None is Python's way of representing "nothing." It is a special value of type NoneType. Many beginners are surprised when their function returns None—this happens because they used print() instead of return.

return Immediately Exits the Function

When Python encounters a return statement, it immediately exits the function. No code after return in the same block will execute:

def check_number(n):
    if n > 0:
        return "Positive"
    elif n < 0:
        return "Negative"
    else:
        return "Zero"
    print("This will never execute")

print(check_number(5))
print(check_number(-3))
print(check_number(0))
Output:
Positive
Negative
Zero

The print() at the end is unreachable code because every branch already returns a value.

Real-Life Analogy

Think of ordering food at a restaurant. You tell the waiter what you want (you call the function with arguments). The kitchen prepares it (the function processes). Then the waiter brings the food back to you (return). That returned food is what you actually consume (use in your program).

If the kitchen only announced "Food ready!" over a speaker (print) but never actually delivered the plate, you would not get anything to eat. Similarly, print() only displays—it does not send a value back for use.

Returning Boolean Values

Functions commonly return True or False to indicate a condition:

def is_adult(age):
    return age >= 18

print(is_adult(20))   # True
print(is_adult(15))   # False
Output:
True
False

This pattern is extremely useful in conditionals: if is_adult(age):.

Early Return Pattern

You can use return to exit a function early based on a condition:

def divide(a, b):
    if b == 0:
        print("Error: Division by zero!")
        return None
    return a / b

print(divide(10, 2))
print(divide(10, 0))
Output:
5.0
Error: Division by zero!
None

The early return prevents the division from happening and safely returns None instead.

Key Points

return sends a value back to the caller.

return immediately exits the function.

A function without return returns None.

print() displays on screen but does not return a value.

You can return multiple values as a tuple.

The returned value can be stored, printed, or used in expressions.

University Exam Tip

The most frequently asked question is:

Difference between print() and return?
  • print() displays output on the screen; return sends a value to the caller.
  • print() cannot be used in further computations; return value can be stored and reused.
  • print() returns None; return returns the specified value.

Practice Questions

Q1. Write a function square(n) that returns the square of a number.

def square(n):
    return n * n

print(square(5))   # 25
print(square(7))   # 49

Q2. What will be the output?

def func():
    return 10
    print("Hello")

result = func()
print(result)
Answer: Only 10 is printed. "Hello" is never reached because return exits immediately.

Q3. Write a function that returns both the area and perimeter of a rectangle.

def rectangle_info(length, width):
    area = length * width
    perimeter = 2 * (length + width)
    return area, perimeter

a, p = rectangle_info(5, 3)
print("Area:", a)         # Area: 15
print("Perimeter:", p)    # Perimeter: 16

Q4. What is the return value of a function that has no return statement?

Answer: Python automatically returns None.

Q5. Write a function is_even(n) that returns True if the number is even, False otherwise.

def is_even(n):
    return n % 2 == 0

print(is_even(4))   # True
print(is_even(7))   # False

Summary

  • The return statement sends a value back to the caller.
  • return immediately exits the function.
  • Without return, a function returns None.
  • print() displays on screen; return sends a value back.
  • Multiple values can be returned using tuples.
  • The returned value can be stored in variables or used in expressions.

Python Programming Handwritten Notes

Master Python Programming with Easy Handwritten Notes – Perfect for Interviews, Placements, GATE & Exams.