Function with Multiple Parameters
Create a function that accepts multiple parameters.
Functions can accept multiple parameters to perform operations on different pieces of data. Understanding parameter order and how to pass multiple values is essential for function design.
📚 Concepts & Theory
Functions can accept multiple parameters, allowing complex operations on different pieces of data. The order of arguments matters when calling the function.
Multiple Parameter Syntax:
def function_name(param1, param2, param3):
# Use all parameters
return result
Simple Example:
def add_numbers(a, b):
return a + b
result = add_numbers(10, 5) # 15
Three or More Parameters:
def calculate_total(price, tax_rate, discount):
tax = price * tax_rate
final = price + tax - discount
return final
total = calculate_total(100, 0.08, 10) # $98
Parameter Order Matters:
def introduce(name, age, city):
return f"{name}, {age}, from {city}"
# Positional - order is critical
introduce("Alice", 30, "NYC") # Correct
introduce(30, "Alice", "NYC") # Wrong! "30, Alice, from NYC"
Keyword Arguments (Order Doesn't Matter):
introduce(city="NYC", name="Alice", age=30) # Works!
Mixing Positional and Keyword:
introduce("Alice", city="NYC", age=30) # Positional first, then keyword
Default Parameters:
def introduce(name, age, city="Unknown"):
return f"{name}, {age}, from {city}"
introduce("Alice", 30) # city defaults to "Unknown"
introduce("Bob", 25, "LA") # Overrides default
Rules for Default Parameters:
- Must come AFTER required parameters
- Can't have required parameter after default
# ❌ WRONG
def func(a=1, b): # SyntaxError!
pass
# ✅ CORRECT
def func(b, a=1):
pass
Common Patterns:
Mathematical Operations:
def calculate_area(length, width):
return length * width
String Formatting:
def format_name(first, last):
return f"{last}, {first}"
Data Processing:
def filter_data(data, min_value, max_value):
return [x for x in data if min_value <= x <= max_value]
Best Practices:
- Limit to 3-5 parameters when possible
- Use descriptive names
- Consider using a dictionary for many parameters
- Group related parameters
🎯 Your Challenge
Create a function full_name(first, last) that returns the full name as "first last".
📝 Starter Code
def full_name(first, last):
pass
- The function needs two parameters: first and last
- Combine them with a space in between
- Use f-string formatting for clean code
- The format should be: first name + space + last name
- Return the combined string
Solution
def full_name(first, last):
return f"{first} {last}"
Explanation
This function accepts two parameters (first and last) and combines them into a single string with a space in between. The f-string f"{first} {last}" concatenates the two parameters efficiently. When called with full_name("John", "Doe"), the parameters are assigned in order (first="John", last="Doe"), and the function returns "John Doe". This demonstrates how multiple parameters allow functions to work with separate pieces of data and combine them meaningfully.
⚠️ Common Mistakes to Avoid
- Reversing the parameter order in the function body
- Forgetting the space between first and last names
- Using + concatenation without proper spacing
- Not returning the result
- Hardcoding names instead of using parameters