Function Parameters: Passing Data to Functions
You'll learn how to make functions flexible and reusable by passing different input data to them.
What Are Function Parameters?
Think of a function as a recipe. The recipe for "make a sandwich" is much more useful if you can specify what kind of sandwich you want each time you use it. Parameters are the ingredients you hand to the function so it knows what to work with.
A parameter is a variable listed in the function declaration that acts as a placeholder for data you'll provide when calling the function. The actual data you pass in is called an argument.
Why Parameters Matter
Without parameters, every function would do exactly the same thing every time. Want to add two numbers? You'd need a different function for every pair! Parameters let you write one function that works with any numbers you give it.
def greet(name): # 'name' is a parameter
print("Hello, " + name)
greet("Alice") # "Alice" is an argument
greet("Bob") # Same function, different result
The function greet can welcome anyone—you just pass a different name each time.
Parameters Enable Reusability
Instead of writing separate functions for similar tasks, write one function with parameters. This reduces code duplication and makes your programs easier to maintain. Change the function once, and all uses benefit.
def add(a, b): # two parameters
return a + b
result1 = add(5, 3) # returns 8
result2 = add(10, 20) # returns 30
Key Takeaway: Parameters are variables in function declarations that accept input data (arguments), making functions flexible and reusable across different situations.