
Python
Learn the difference between parameters and arguments, and how to make functions flexible by accepting data.
These terms are often confused, but understanding the distinction is crucial for writing flexible code. Parameters are the placeholders you define in the function definitionβthey're the "slots" waiting to receive data. Arguments are the actual values you pass when calling the functionβthey're the data that fills those slots.
def greet(name): # 'name' is a PARAMETER
print(f"Hello {name}!")
greet("Alice") # "Alice" is an ARGUMENTdef greet(name):
print(f"Hello {name}!")
greet("Alice") # Pass "Alice"
greet("Bob") # Pass "Bob"
greet("Charlie") # Pass "Charlie"Output:
Hello Alice!
Hello Bob!
Hello Charlie!def greet_person(name, age):
print(f"Hello {name}!")
print(f"You are {age} years old.")
print("Welcome to our course!")
greet_person("Alice", 25)
greet_person("Bob", 30)Output:
Hello Alice!
You are 25 years old.
Welcome to our course!
Hello Bob!
You are 30 years old.
Welcome to our course!When you call a function, Python matches arguments to parameters based on their position from left to right. The first argument you pass goes to the first parameter, the second argument to the second parameter, and so on. This positional matching means that passing arguments in the wrong order will produce incorrect results or type errors, so parameter order is critical.
def introduce(first, last, age):
print(f"{first} {last} is {age} years old")
# Correct order
introduce("John", "Doe", 30) # β
# Wrong order gives wrong results
introduce("30", "Doe", "John") # β Weird output!def calculate_area(length, width):
area = length * width
print(f"Length: {length}, Width: {width}")
print(f"Area: {area}")
calculate_area(5, 10) # Area: 50
calculate_area(3, 7) # Area: 21def show_average(num1, num2, num3):
average = (num1 + num2 + num3) / 3
print(f"Numbers: {num1}, {num2}, {num3}")
print(f"Average: {average}")
show_average(85, 90, 88)
show_average(100, 95, 92)Parameters are what make functions truly powerful and reusable. Without parameters, a function is hardcoded for specific values and must be edited every time you need different data. With parameters, a single function becomes a template that works with infinite variations of input, making your code DRY (Don't Repeat Yourself) and maintainable.
def show_score():
score = 85
print(f"Your score: {score}")This only works for score 85. To use a different score, you'd need to modify the function!
def show_score(score):
print(f"Your score: {score}")
show_score(85) # Works for 85
show_score(92) # Works for 92
show_score(78) # Works for 78Same function, different data! Much better! β¨
Choose descriptive parameter names:
# β
Good - Clear names
def calculate_total(price, tax_rate):
total = price + (price * tax_rate)
return total
# β Bad - Unclear names
def calculate_total(a, b):
total = a + (a * b)
return totaldef greet(name, age):
print(f"{name} is {age}")
greet("Alice") # β Missing age argument!
greet("Bob", 30, "ok") # β Too many arguments!
greet("Charlie", 25) # β
Correct!def add_numbers(a, b):
return a + b
add_numbers(5, 3) # β
Works
add_numbers("5", "3") # Returns "53" not 8!
add_numbers(5, "3") # β Error!1. Use clear parameter names β Self-documenting code
2. Keep functions focused β Not too many parameters
3. Parameter order matters β Be consistent
4. Document parameters β Tell users what each does
def send_email(to, subject, message):
"""
Send an email.
to: Email address (string)
subject: Email subject (string)
message: Email body (string)
"""
print(f"Sending email to {to}")
print(f"Subject: {subject}")
print(f"Message: {message}")| Concept | Remember |
|---|---|
| Parameters | Variables in function definition |
| Arguments | Actual values when calling |
| Position | Arguments matched by position |
| Flexibility | Parameters make functions reusable |
| Clarity | Use descriptive parameter names |
| Count | Provide correct number of arguments |
Now that functions can accept data via parameters, let's learn how to send data BACK from functions using return statements.
Practice: Try challenges with parameters
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep β’ Build Real β’ Verify Skills β’ Launch Forward