Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconPython Programming Unlocked for Beginners
Python Programming Unlocked for Beginners

Chapter 5: Functions

5.3: Return Values

In the previous topics, we have covered the essentials of defining functions and function arguments. In this topic, we will delve deeper into the concept of return values.

Return values are an integral part of Python functions. When a function is executed, it can return a value to the caller. This value can then be used for further processing or passed on as input to another function.

To return a value from a function, the return statement is used, which is followed by the value or expression that needs to be returned. It is important to note that the return statement marks the end of the function, and the control is then passed back to the caller. This feature is particularly useful when you want to avoid repeated code or when you want to pass on a result to another function. 

The general syntax for a function with a return value is:

def function_name(parameters):
    ...
    return value

Let's take a look at some examples to better understand how return values work.

Example 1: Simple addition function

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

sum_result = add(5, 7)
print(sum_result)  # Output: 12

In this example, the add function takes two parameters, a and b, adds them together, and returns the result. When we call the add function with the arguments 5 and 7, the returned value (12) is assigned to the variable sum_result.

Example 2: Maximum of two numbers

def maximum(a, b):
    if a > b:
        return a
    else:
        return b

max_value = maximum(10, 15)
print(max_value)  # Output: 15

Here, we define a maximum function that takes two parameters, a and b, and returns the greater one. The function uses an if-else statement to determine which value is greater and returns it accordingly.

Example 3: Return multiple values

You can also return multiple values from a function using a tuple, list, or dictionary.

def min_max(numbers):
    return min(numbers), max(numbers)

values = [5, 2, 8, 1, 10]
minimum, maximum = min_max(values)
print(f"Minimum: {minimum}, Maximum: {maximum}")  # Output: Minimum: 1, Maximum: 10

In this example, we have a function min_max that takes a list of numbers as input and returns a tuple containing the minimum and maximum values. When the function is called with a list, it returns the tuple, which we then unpack into the minimum and maximum variables. 

Remember that once a return statement is executed, the function exits immediately. If there is code after the return statement, it won't be executed.

Exercise 5.3.1: Calculate Area of a Rectangle

Write a function called area_of_rectangle that takes two arguments, length and width, and returns the area of a rectangle.

Instructions:

  1. Define the function area_of_rectangle.
  2. Calculate the area of the rectangle.
  3. Return the calculated area.
def area_of_rectangle(length, width):
    area = length * width 
    return area

length = 5
width = 10
result = area_of_rectangle(length, width)
print(f"The area of the rectangle is {result}")  # Output: The area of the rectangle is 50

Exercise 5.3.2: Check if a Number is Even or Odd

Create a function called is_even that takes a single argument, number, and returns True if the number is even and False otherwise.

Instructions:

  1. Define the function is_even.
  2. Use the modulo operator to check if the number is even.
  3. Return True if the number is even and False otherwise.
def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

num = 7
result = is_even(num)
print(f"Is {num} even? {result}")  # Output: Is 7 even? False

Exercise 5.3.3: Get the Length of a String

Write a function called string_length that takes a single argument, string, and returns the length of the string.

Instructions:

  1. Define the function string_length.
  2. Use the len() function to find the length of the string.
  3. Return the length of the string.
def string_length(string):
    length = len(string)
    return length

text = "Python is awesome!"
result = string_length(text)
print(f"The length of the string is {result}")  # Output: The length of the string is 18

5.3: Return Values

In the previous topics, we have covered the essentials of defining functions and function arguments. In this topic, we will delve deeper into the concept of return values.

Return values are an integral part of Python functions. When a function is executed, it can return a value to the caller. This value can then be used for further processing or passed on as input to another function.

To return a value from a function, the return statement is used, which is followed by the value or expression that needs to be returned. It is important to note that the return statement marks the end of the function, and the control is then passed back to the caller. This feature is particularly useful when you want to avoid repeated code or when you want to pass on a result to another function. 

The general syntax for a function with a return value is:

def function_name(parameters):
    ...
    return value

Let's take a look at some examples to better understand how return values work.

Example 1: Simple addition function

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

sum_result = add(5, 7)
print(sum_result)  # Output: 12

In this example, the add function takes two parameters, a and b, adds them together, and returns the result. When we call the add function with the arguments 5 and 7, the returned value (12) is assigned to the variable sum_result.

Example 2: Maximum of two numbers

def maximum(a, b):
    if a > b:
        return a
    else:
        return b

max_value = maximum(10, 15)
print(max_value)  # Output: 15

Here, we define a maximum function that takes two parameters, a and b, and returns the greater one. The function uses an if-else statement to determine which value is greater and returns it accordingly.

Example 3: Return multiple values

You can also return multiple values from a function using a tuple, list, or dictionary.

def min_max(numbers):
    return min(numbers), max(numbers)

values = [5, 2, 8, 1, 10]
minimum, maximum = min_max(values)
print(f"Minimum: {minimum}, Maximum: {maximum}")  # Output: Minimum: 1, Maximum: 10

In this example, we have a function min_max that takes a list of numbers as input and returns a tuple containing the minimum and maximum values. When the function is called with a list, it returns the tuple, which we then unpack into the minimum and maximum variables. 

Remember that once a return statement is executed, the function exits immediately. If there is code after the return statement, it won't be executed.

Exercise 5.3.1: Calculate Area of a Rectangle

Write a function called area_of_rectangle that takes two arguments, length and width, and returns the area of a rectangle.

Instructions:

  1. Define the function area_of_rectangle.
  2. Calculate the area of the rectangle.
  3. Return the calculated area.
def area_of_rectangle(length, width):
    area = length * width 
    return area

length = 5
width = 10
result = area_of_rectangle(length, width)
print(f"The area of the rectangle is {result}")  # Output: The area of the rectangle is 50

Exercise 5.3.2: Check if a Number is Even or Odd

Create a function called is_even that takes a single argument, number, and returns True if the number is even and False otherwise.

Instructions:

  1. Define the function is_even.
  2. Use the modulo operator to check if the number is even.
  3. Return True if the number is even and False otherwise.
def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

num = 7
result = is_even(num)
print(f"Is {num} even? {result}")  # Output: Is 7 even? False

Exercise 5.3.3: Get the Length of a String

Write a function called string_length that takes a single argument, string, and returns the length of the string.

Instructions:

  1. Define the function string_length.
  2. Use the len() function to find the length of the string.
  3. Return the length of the string.
def string_length(string):
    length = len(string)
    return length

text = "Python is awesome!"
result = string_length(text)
print(f"The length of the string is {result}")  # Output: The length of the string is 18

5.3: Return Values

In the previous topics, we have covered the essentials of defining functions and function arguments. In this topic, we will delve deeper into the concept of return values.

Return values are an integral part of Python functions. When a function is executed, it can return a value to the caller. This value can then be used for further processing or passed on as input to another function.

To return a value from a function, the return statement is used, which is followed by the value or expression that needs to be returned. It is important to note that the return statement marks the end of the function, and the control is then passed back to the caller. This feature is particularly useful when you want to avoid repeated code or when you want to pass on a result to another function. 

The general syntax for a function with a return value is:

def function_name(parameters):
    ...
    return value

Let's take a look at some examples to better understand how return values work.

Example 1: Simple addition function

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

sum_result = add(5, 7)
print(sum_result)  # Output: 12

In this example, the add function takes two parameters, a and b, adds them together, and returns the result. When we call the add function with the arguments 5 and 7, the returned value (12) is assigned to the variable sum_result.

Example 2: Maximum of two numbers

def maximum(a, b):
    if a > b:
        return a
    else:
        return b

max_value = maximum(10, 15)
print(max_value)  # Output: 15

Here, we define a maximum function that takes two parameters, a and b, and returns the greater one. The function uses an if-else statement to determine which value is greater and returns it accordingly.

Example 3: Return multiple values

You can also return multiple values from a function using a tuple, list, or dictionary.

def min_max(numbers):
    return min(numbers), max(numbers)

values = [5, 2, 8, 1, 10]
minimum, maximum = min_max(values)
print(f"Minimum: {minimum}, Maximum: {maximum}")  # Output: Minimum: 1, Maximum: 10

In this example, we have a function min_max that takes a list of numbers as input and returns a tuple containing the minimum and maximum values. When the function is called with a list, it returns the tuple, which we then unpack into the minimum and maximum variables. 

Remember that once a return statement is executed, the function exits immediately. If there is code after the return statement, it won't be executed.

Exercise 5.3.1: Calculate Area of a Rectangle

Write a function called area_of_rectangle that takes two arguments, length and width, and returns the area of a rectangle.

Instructions:

  1. Define the function area_of_rectangle.
  2. Calculate the area of the rectangle.
  3. Return the calculated area.
def area_of_rectangle(length, width):
    area = length * width 
    return area

length = 5
width = 10
result = area_of_rectangle(length, width)
print(f"The area of the rectangle is {result}")  # Output: The area of the rectangle is 50

Exercise 5.3.2: Check if a Number is Even or Odd

Create a function called is_even that takes a single argument, number, and returns True if the number is even and False otherwise.

Instructions:

  1. Define the function is_even.
  2. Use the modulo operator to check if the number is even.
  3. Return True if the number is even and False otherwise.
def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

num = 7
result = is_even(num)
print(f"Is {num} even? {result}")  # Output: Is 7 even? False

Exercise 5.3.3: Get the Length of a String

Write a function called string_length that takes a single argument, string, and returns the length of the string.

Instructions:

  1. Define the function string_length.
  2. Use the len() function to find the length of the string.
  3. Return the length of the string.
def string_length(string):
    length = len(string)
    return length

text = "Python is awesome!"
result = string_length(text)
print(f"The length of the string is {result}")  # Output: The length of the string is 18

5.3: Return Values

In the previous topics, we have covered the essentials of defining functions and function arguments. In this topic, we will delve deeper into the concept of return values.

Return values are an integral part of Python functions. When a function is executed, it can return a value to the caller. This value can then be used for further processing or passed on as input to another function.

To return a value from a function, the return statement is used, which is followed by the value or expression that needs to be returned. It is important to note that the return statement marks the end of the function, and the control is then passed back to the caller. This feature is particularly useful when you want to avoid repeated code or when you want to pass on a result to another function. 

The general syntax for a function with a return value is:

def function_name(parameters):
    ...
    return value

Let's take a look at some examples to better understand how return values work.

Example 1: Simple addition function

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

sum_result = add(5, 7)
print(sum_result)  # Output: 12

In this example, the add function takes two parameters, a and b, adds them together, and returns the result. When we call the add function with the arguments 5 and 7, the returned value (12) is assigned to the variable sum_result.

Example 2: Maximum of two numbers

def maximum(a, b):
    if a > b:
        return a
    else:
        return b

max_value = maximum(10, 15)
print(max_value)  # Output: 15

Here, we define a maximum function that takes two parameters, a and b, and returns the greater one. The function uses an if-else statement to determine which value is greater and returns it accordingly.

Example 3: Return multiple values

You can also return multiple values from a function using a tuple, list, or dictionary.

def min_max(numbers):
    return min(numbers), max(numbers)

values = [5, 2, 8, 1, 10]
minimum, maximum = min_max(values)
print(f"Minimum: {minimum}, Maximum: {maximum}")  # Output: Minimum: 1, Maximum: 10

In this example, we have a function min_max that takes a list of numbers as input and returns a tuple containing the minimum and maximum values. When the function is called with a list, it returns the tuple, which we then unpack into the minimum and maximum variables. 

Remember that once a return statement is executed, the function exits immediately. If there is code after the return statement, it won't be executed.

Exercise 5.3.1: Calculate Area of a Rectangle

Write a function called area_of_rectangle that takes two arguments, length and width, and returns the area of a rectangle.

Instructions:

  1. Define the function area_of_rectangle.
  2. Calculate the area of the rectangle.
  3. Return the calculated area.
def area_of_rectangle(length, width):
    area = length * width 
    return area

length = 5
width = 10
result = area_of_rectangle(length, width)
print(f"The area of the rectangle is {result}")  # Output: The area of the rectangle is 50

Exercise 5.3.2: Check if a Number is Even or Odd

Create a function called is_even that takes a single argument, number, and returns True if the number is even and False otherwise.

Instructions:

  1. Define the function is_even.
  2. Use the modulo operator to check if the number is even.
  3. Return True if the number is even and False otherwise.
def is_even(number):
    if number % 2 == 0:
        return True
    else:
        return False

num = 7
result = is_even(num)
print(f"Is {num} even? {result}")  # Output: Is 7 even? False

Exercise 5.3.3: Get the Length of a String

Write a function called string_length that takes a single argument, string, and returns the length of the string.

Instructions:

  1. Define the function string_length.
  2. Use the len() function to find the length of the string.
  3. Return the length of the string.
def string_length(string):
    length = len(string)
    return length

text = "Python is awesome!"
result = string_length(text)
print(f"The length of the string is {result}")  # Output: The length of the string is 18