In Python, functions can also return values using the return
statement. The return
statement is used to exit a function and return a value to the caller.
Here’s an example of a function that calculates the area of a rectangle and returns the result:
def calculate_area(length, width): area = length * width return area result = calculate_area(5, 3) print(result) # Output: 15
In the above example, the calculate_area()
the function takes two arguments length
and width
. It calculates the area of the rectangle using the formula area = length * width
and then returns the result using the return
statement.
The returned value is assigned to the variable result
which is then printed using the print()
function.
Functions can also return multiple values by separating them with commas in the return statement. Here’s an example:
def calculate_area_and_perimeter(length, width): area = length * width perimeter = 2 * (length + width) return area, perimeter result1, result2 = calculate_area_and_perimeter(5, 3) print(result1) # Output: 15 print(result2) # Output: 16
In the above example, the calculate_area_and_perimeter()
the function takes two arguments length
and width
. It calculates the area and perimeter of the rectangle and then returns them as a tuple using the return
statement.
The returned tuple is then unpacked into the variables result1
and result2
which are then printed using the print()
function.
You can also use the return
statement without any value to exit a function without returning anything. This is useful in cases where you want to exit a function early or when you don’t need to return any value from the function.
Here’s an example of a function that prints the multiplication table of a given number:
def print_multiplication_table(number): for i in range(1, 11): result = number * i print(f"{number} x {i} = {result}") print_multiplication_table(5)
In the above example, the print_multiplication_table()
function takes a single argument number
. It then uses a for
loop to print the multiplication table of the given number from 1 to 10.
The function does not use the return
statement as it is not necessary to return any value from the function. Instead, it uses the print()
function to display the results directly to the user.
Functions can also have default return values using the return
statement without any value. Here’s an example:
def check_even(number): if number % 2 == 0: return True else: return False result = check_even(5) print(result) # Output: False
In the above example, the check_even()
the function takes a single argument number
. It checks whether the given number is even or odd and then returns True
if it is even and False
if it is odd.
The function uses the return
statement to return a default value of None
if the given number is not even or odd. This is because the return
the statement is only called within the if
and else
blocks, and if the condition is not met, the function does not have any other return
statement.
The returned value is assigned to the variable result
which is then printed using the print()
function. In this case, the output is False
as the given number 5
is not even.