Tuples in Functions

Tuples are commonly used in functions to return multiple values and to accept variable-length arguments.

Returning Multiple Values

Example 1: Function Returning Multiple Values

# Function returning multiple values
def get_person_info():
    name = "Karthick AG"
    age = 30
    country = "India"
    return name, age, country

person_info = get_person_info()
print("Person Info:", person_info)

Output

Person Info: ('Karthick AG', 30, 'India')

Example 2: Unpacking Function Return Values

# Unpacking returned tuple
name, age, country = get_person_info()
print(f"Name: {name}, Age: {age}, Country: {country}")

Output

Name: Karthick AG, Age: 30, Country: India

Using *args in Functions

Example 3: Function with Variable-Length Arguments

# Using *args to accept variable-length arguments
def greet(*names):
    for name in names:
        print(f"Hello, {name}!")

greet("Karthick AG", "Durai", "Vijay")

Output

Hello, Karthick AG!

Hello, Durai!

Hello, Vijay!

Explanation: The *args parameter allows the function to accept any number of positional arguments, which are received as a tuple.