A return statement is used to end the execution of the function call and it "returns" the value of the expression following the return keyword to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to invoke a function so that the passed statements can be executed.
Example:
Python
def add(a, b):
# returning sum of a and b
return a + b
def is_true(a):
# returning boolean of a
return bool(a)
# calling function
res = add(2, 3)
print(res)
res = is_true(2<5)
print(res)
Explanation:
- add(a, b) Function: Takes two arguments a and b. Returns the sum of a and b.
- is_true(a) Function: Takes one argument a. Returns the boolean value of a.
- Function Calls: res = add(2, 3) computes the sum of 2 and 3, storing the result (5) in res. res = is_true(2 < 5) evaluates the expression 2 < 5 (which is True) and stores the boolean value True in res.
Let's explore python return statement in detail:
Syntax:
def function_name(parameters):
# Function body
return value
When the return statement is executed, the function terminates and the specified value is returned to the caller. If no value is specified, the function returns None by default.
Note:
Note: Return statement can not be used outside the function.
Returning Multiple Values
Python allows you to return multiple values from a function by returning them as a tuple:
Example:
Python
def fun():
name = "Alice"
age = 30
return name, age
name, age = fun()
print(name)
print(age) # Output: 30
In this example, the fun() function returns two values: name and age. The caller unpacks these values into separate variables.
Returning List
We can also return more complex data structures such as lists or dictionaries from a function:
Python
def fun(n):
return [n**2, n**3]
res = fun(3)
print(res)
In this case, the function fun() returns a list containing the square and cube of the input number.
Function returning another function
In Python, functions are first-class citizens, meaning you can return a function from another function. This is useful for creating higher-order functions.
Here's an example of a function that returns another function:
Python
def fun1(msg):
def fun2():
# Using the outer function's message
return f"Message: {msg}"
return fun2
# Getting the inner function
fun3 = fun1("Hello, World!")
# Calling the inner function
print(fun3())
OutputMessage: Hello, World!
Similar Reads
Python break statement The break statement in Python is used to exit or "break" out of a loop (either a for or while loop) prematurely, before the loop has iterated through all its items or reached its condition. When the break statement is executed, the program immediately exits the loop, and the control moves to the nex
5 min read
Jump Statements in Python In any programming language, a command written by the programmer for the computer to act is known as a statement. In simple words, a statement can be thought of as an instruction that programmers give to the computer and upon receiving them, the computer acts accordingly. There are various types of
4 min read
Python Match Case Statement Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement
7 min read
Nested-if statement in Python For more complex decision trees, Python allows for nested if statements where one if statement is placed inside another. This article will explore the concept of nested if statements in Python, providing clarity on how to use them effectively.Python Nested if StatementA nested if statement in Python
2 min read
Loop Control Statements Loop control statements in Python are special statements that help control the execution of loops (for or while). They let you modify the default behavior of the loop, such as stopping it early, skipping an iteration, or doing nothing temporarily. . Python supports the following control statements:B
4 min read