Nearby lessons

71 of 159

Python - Return Statement

📌 What You Will Learn
  • Understand what the return statement is and how it terminates a function
  • Return values, expressions, strings, booleans and lists from functions
  • Compare print() with return and understand None
  • Return multiple values from a function
  • Receive multiple returned values using tuple unpacking

Return Statement

When a function completes its execution, sometimes it needs to send a result back to the function caller.

To send the result from a function, Python provides the return statement.

The return statement terminates the execution of the function and transfers control back to the calling statement.

If a value is specified after the return keyword, that value is returned to the caller.

Why Do We Need the Return Statement?

Consider a function that adds two numbers.

If the function only prints the result, the result cannot be used elsewhere in the program.

If the function returns the result, it can be stored in a variable, used in calculations, or passed to another function.

Therefore, returning values makes functions more useful and reusable.

Syntax

🐍Code Cell
1def function_name(parameters):
2 statements
3 
4 return value
Output
No output captured.

Syntax Explanation

Statement Purpose
return value Returns the specified value to the caller.
return Terminates the function without returning any value.

Example 1 - Function Returning a Value

🐍Code Cell
1def add(a, b):
2 return a + b
3 
4result = add(10, 20)
5 
6print(result)
Output
30

Explanation

The function receives two numbers.

It calculates their sum and returns the result.

The returned value is stored in the variable result.

Finally, the value is printed.

Example 2 - Returning an Expression

🐍Code Cell
1def square(n):
2 return n * n
3 
4print(square(5))
5 
6print(square(10))
Output
25
100

Explanation

The expression n * n is evaluated first.

The calculated value is then returned to the caller.

Example 3 - Returning a String

🐍Code Cell
1def message():
2 return "Welcome to Python"
3 
4text = message()
5 
6print(text)
Output
Welcome to Python

Example 4 - Returning a Boolean Value

🐍Code Cell
1def isEven(num):
2 return num % 2 == 0
3 
4print(isEven(10))
5 
6print(isEven(15))
Output
True
False

Example 5 - Returning a List

🐍Code Cell
1def colors():
2 return ["Red", "Green", "Blue"]
3 
4data = colors()
5 
6print(data)
Output
['Red', 'Green', 'Blue']

Example 6 - Function Without Return Statement

🐍Code Cell
1def hello():
2 print("Hello")
3 
4result = hello()
5 
6print(result)
Output
Hello
None

Explanation

The function prints the message.

Since there is no return statement, Python automatically returns None.

Therefore, the variable result stores None.

Example 7 - Empty Return Statement

🐍Code Cell
1def demo():
2 print("Start")
3 
4 return
5 
6 print("End")
7 
8demo()
Output
Start

Explanation

When Python encounters the return statement, the function immediately terminates.

The statement after return is never executed.

Flow of Return Statement

Step Action
1 Function is called.
2 Function executes its statements.
3 return sends the result back.
4 Function execution stops.
5 Control returns to the caller.

Difference Between print() and return

print() return
Displays output on the screen. Returns a value to the caller.
Does not terminate the function. Terminates the function immediately.
Cannot be reused directly. The returned value can be reused anywhere.
Mainly used for displaying information. Mainly used for sending results back.

Real-World Applications

  • Returning calculated values from mathematical functions.
  • Returning database query results.
  • Returning API responses.
  • Returning validation results.
  • Returning processed data from helper functions.

Returning Multiple Values from a Function

In the previous section, we learned how a function can return a single value by using the return statement.

Python also allows a function to return multiple values.

This is one of the powerful features of Python because many programming languages can return only one value directly.

When multiple values are returned, Python automatically packs them into a Tuple.

Why Return Multiple Values?

Sometimes a function performs multiple calculations and needs to send several results back to the caller.

For example:

  • Returning addition, subtraction, multiplication and division together.
  • Returning student name, marks and grade.
  • Returning minimum and maximum values.
  • Returning multiple statistics from a dataset.

Instead of calling multiple functions, Python allows us to return all results together.

Syntax

🐍Code Cell
1def function_name():
2 
3 
4 return value1, value2, value3
Output
No output captured.

Important Point

Although multiple values appear to be returned, Python actually returns a single Tuple.

Python automatically performs Tuple Packing.

Example 1 - Returning Multiple Values

🐍Code Cell
1def calculate(a, b):
2 return a+b, a-b, a*b, a/b
3 
4result = calculate(20, 10)
5 
6print(result)
7 
8print(type(result))
Output
(30, 10, 200, 2.0)

Explanation

The function returns four values.

Python automatically packs these values into a tuple.

Therefore, the variable result stores a tuple.

Tuple Packing

When multiple values are returned, Python automatically creates a tuple.

This process is called Tuple Packing.

Example

🐍Code Cell
1def demo():
2 return 10, 20, 30
3 
4data = demo()
5 
6print(data)
7 
8print(type(data))
Output
(10, 20, 30)

Receiving Multiple Returned Values

Instead of storing the returned tuple in a single variable, we can receive each value into separate variables.

This process is called Tuple Unpacking.

Example

🐍Code Cell
1def calculate(a, b):
2 return a+b, a-b, a*b, a/b
3 
4sumValue, subValue, mulValue, divValue = calculate(20, 10)
5 
6print("Addition =", sumValue)
7 
8print("Subtraction =", subValue)
9 
10print("Multiplication =", mulValue)
11 
12print("Division =", divValue)
Output
Addition = 30
Subtraction = 10
Multiplication = 200
Division = 2.0

Explanation

The returned tuple contains four values.

Each value is automatically assigned to the corresponding variable.

This process is known as Tuple Unpacking.

Example 2 - Returning Student Information

🐍Code Cell
1def student():
2 return "Rahul", 90, "A"
3 
4name, marks, grade = student()
5 
6print(name)
7 
8print(marks)
9 
10print(grade)
Output
Rahul
90
A

Example 3 - Returning Minimum and Maximum Values

🐍Code Cell
1def minMax(numbers):
2 return min(numbers), max(numbers)
3 
4minimum, maximum = minMax([10, 50, 30, 80, 20])
5 
6print("Minimum =", minimum)
7 
8print("Maximum =", maximum)
Output
Minimum = 10
Maximum = 80

Example 4 - Returning Different Data Types

🐍Code Cell
1def details():
2 return 101, "Python", True, 95.5
3 
4data = details()
5 
6print(data)
Output
(101, 'Python', True, 95.5)

Example 5 - Returning a Tuple Explicitly

🐍Code Cell
1def demo():
2 return (1, 2, 3)
3 
4print(demo())
Output
(1, 2, 3)

Automatic Tuple Packing

Return Statement Python Stores As
return 10,20 (10,20)
return 10,20,30 (10,20,30)
return "A",100 ("A",100)
return True,50.5 (True,50.5)

Difference Between Single Return and Multiple Return

Single Return Multiple Return
Returns one value. Returns multiple values.
Any data type. Automatically packed into a tuple.
Stored in one variable. Can be stored in one variable or unpacked into multiple variables.

Real-World Applications

  • Returning multiple calculation results.
  • Returning database records.
  • Returning API response values.
  • Returning student information.
  • Returning minimum and maximum values.
  • Returning statistical information.
📝 Key Takeaways
  • The return statement sends a result back to the caller and ends the function
  • A function without a return statement returns None
  • print() only displays output while return makes the result usable in code
  • Functions can return multiple values as an automatically packed tuple
  • Tuple unpacking receives multiple returned values

🧠 Test Your Knowledge

16 Questions
Progress: 0 / 16