Python Program to Return Multiple Values from a Function

Introduction

In Python, a function can return more than one value by using a tuple, list, or dictionary. This allows you to return multiple results from a single function call, which is useful in scenarios where a function needs to provide several related outcomes. This tutorial will guide you through creating a Python program that returns multiple values from a function.

Example:

  • Function Name: calculate
  • Function Purpose: Return the sum and product of two numbers.
  • Program Output:
    Sum: 15, Product: 50
    

Problem Statement

Create a Python program that:

  • Defines a function named calculate.
  • The function takes two arguments: a and b.
  • The function calculates the sum and product of the two numbers.
  • The function returns both the sum and the product.
  • The program calls the function and displays the returned values.

Solution Steps

  1. Define the Function: Use the def keyword to define a function named calculate.
  2. Add Parameters: Add two parameters, a and b, to accept two numbers.
  3. Add Function Logic: Inside the function, calculate the sum and product of a and b.
  4. Return Multiple Values: Use a tuple to return both the sum and the product.
  5. Call the Function: Call the function and unpack the returned values into separate variables.
  6. Display the Returned Values: Print the returned sum and product.

Python Program

# Python Program to Return Multiple Values from a Function
# Author: https://www.rameshfadatare.com/

# Step 1: Define the function
def calculate(a, b):
    # Step 3: Add function logic
    sum_result = a + b
    product_result = a * b
    # Step 4: Return multiple values as a tuple
    return sum_result, product_result

# Step 5: Call the function and unpack the returned values
sum_value, product_value = calculate(10, 5)

# Step 6: Display the returned values
print(f"Sum: {sum_value}, Product: {product_value}")

Explanation

Step 1: Define the Function

  • The function is defined using the def keyword, followed by the function name calculate.
  • The function accepts two parameters, a and b, which are the numbers for which the sum and product will be calculated.

Step 2: Add Parameters

  • The parameters a and b allow the function to accept two numbers as input.

Step 3: Add Function Logic

  • Inside the function, the sum and product of a and b are calculated using basic arithmetic operations (+ and *).
  • The results are stored in the variables sum_result and product_result.

Step 4: Return Multiple Values

  • The function returns both sum_result and product_result as a tuple. In Python, returning multiple values is as simple as separating them with commas.

Step 5: Call the Function

  • The function calculate(10, 5) is called with the arguments 10 and 5. The returned tuple is unpacked into the variables sum_value and product_value.

Step 6: Display the Returned Values

  • The print() function is used to display the sum and product that were returned by the function.

Output Example

Example Output:

Sum: 15, Product: 50

Additional Examples

Example 1: Returning Multiple Values as a List

# Returning multiple values as a list
def calculate(a, b):
    sum_result = a + b
    product_result = a * b
    # Returning the results as a list
    return [sum_result, product_result]

results = calculate(7, 3)
print(f"Sum: {results[0]}, Product: {results[1]}")

Output:

Sum: 10, Product: 21

Example 2: Returning Multiple Values as a Dictionary

# Returning multiple values as a dictionary
def calculate(a, b):
    sum_result = a + b
    product_result = a * b
    # Returning the results as a dictionary
    return {"sum": sum_result, "product": product_result}

results = calculate(8, 2)
print(f"Sum: {results['sum']}, Product: {results['product']}")

Output:

Sum: 10, Product: 16

Example 3: Returning Multiple Values without Unpacking

# Returning multiple values without unpacking
def calculate(a, b):
    sum_result = a + b
    product_result = a * b
    return sum_result, product_result

# Receiving the result as a tuple
results = calculate(12, 4)
print(f"Sum: {results[0]}, Product: {results[1]}")

Output:

Sum: 16, Product: 48

Conclusion

This Python program demonstrates how to return multiple values from a function using different data structures like tuples, lists, and dictionaries. Returning multiple values is a powerful feature in Python, allowing functions to provide more complex outputs and making your code more flexible. Understanding how to return and handle multiple values from functions is essential for writing efficient and versatile Python programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top