Python Program to Return Multiple Values From a Function


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

In programming, it is common to have functions that need to return more than one value to the caller. While some programming languages may not directly support returning multiple values from a function, there are several ways to accomplish this.


Python Code :

In Python, you can return multiple values from a function using a tuple. Here’s an example of how to do it:


def get_name_and_age():
    name = input("What's your name? ")
    age = input("How old are you? ")
    return name, age

# Call the function and store the returned values in variables
user_name, user_age = get_name_and_age()

# Print the returned values
print("Your name is", user_name)
print("You are", user_age, "years old.")

In this example, we define a function called get_name_and_age that prompts the user for their name and age, and returns both values as a tuple.

We then call the function and store the returned values in variables user_name and user_age. We can then print out these values using print statements.

Note that when returning multiple values from a function, you don’t need to explicitly create a tuple - Python will automatically pack the values into a tuple for you.


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co