Parameters and Arguments
Overview
Functions often need input values to work effectively. You'll explore how to define parameters, pass arguments, and distinguish between positional and keyword arguments for more flexibility, enabling you to create more versatile and reusable code.
What You Will Learn in This Lesson
By the end of this lesson, you will know:
- What parameters are: Understand how to define functions that accept input values.
- What arguments are: Learn how to pass values to functions when calling them.
- The difference: Distinguish between parameters (in the definition) and arguments (in the call).
- Multiple parameters: Create functions that accept multiple inputs.
Why This Matters
Parameters and arguments let you create flexible functions that can work with different data. Instead of hardcoding values inside a function, you can pass them in when you call it. This makes functions reusable and powerful - the same function can work with many different values.
Step 1: What Are Parameters?
Parameters are variables that you define in the function definition. They act as placeholders for values that will be passed to the function when it's called. Think of parameters like empty boxes - you're telling Python "this function will receive some values, and I'll call them by these names."
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet("Bob")
Define the Parameter
In the function definition, write the parameter name inside the parentheses: def greet(name):. The parameter name is a variable that will hold whatever value you pass when calling the function.
Use the Parameter
Inside the function body, you can use the parameter just like any other variable. When you write print("Hello, " + name + "!"), Python will use whatever value was passed in for name.
Pass an Argument
When you call the function, you pass a value (called an argument) inside the parentheses: greet("Alice"). The string "Alice" is the argument that gets assigned to the name parameter.
Key Concept: Parameters are defined in the function definition (like def greet(name):), while arguments are the actual values you pass when calling the function (like greet("Alice")). The parameter name receives the argument "Alice".
Mini Practice #1: Your First Parameter
Try It YourselfTry calling the function with different names. Notice how the same function works with different values:
What happened? When Python sees greet("Alice"), it takes the argument "Alice" and assigns it to the parameter name. Then inside the function, when Python encounters name, it uses the value "Alice". That's why "Hello, Alice!" is printed. When you call greet("Bob"), the parameter name now receives "Bob", so "Hello, Bob!" is printed. The same function works with different arguments - that's the power of parameters!
Step 2: Parameters vs Arguments
It's important to understand the difference between parameters and arguments, even though people sometimes use these terms interchangeably:
Parameters
Defined in the function definition. They're the variable names inside the parentheses.
def calculate_area(length, width):
# length and width are parameters
return length * width
Parameters are placeholders
Arguments
Passed when calling the function. They're the actual values you provide.
calculate_area(5, 3)
# 5 and 3 are arguments
Arguments are actual values
Memory Trick
Think of it this way: Parameters are like empty boxes with labels (defined in the function), and arguments are the actual items you put in those boxes (passed when calling). When you call calculate_area(5, 3), the argument 5 goes into the length parameter box, and 3 goes into the width parameter box.
Step 3: Multiple Parameters
Functions can have multiple parameters. Just separate them with commas. When you call the function, you must provide arguments for all parameters in the same order they're defined.
def introduce(name, age, city):
print("My name is " + name)
print("I am " + str(age) + " years old")
print("I live in " + city)
introduce("Alice", 25, "New York")
introduce("Bob", 30, "London")
Define Multiple Parameters
List them separated by commas: def introduce(name, age, city):. The order matters - the first argument you pass will go to the first parameter, the second argument to the second parameter, and so on.
Pass Arguments in Order
When calling introduce("Alice", 25, "New York"), "Alice" goes to name, 25 goes to age, and "New York" goes to city. The order must match!
Important: When you have multiple parameters, the order of arguments matters. If you call introduce(25, "Alice", "New York") (wrong order), Python will assign 25 to name (which expects a string), causing an error or unexpected behavior. Always pass arguments in the same order as the parameters are defined.
Mini Practice #2: Multiple Parameters
Try It YourselfTry calling the function with different values. Notice how each argument goes to its corresponding parameter:
What happened? When you call calculate_total(10, 3), Python assigns 10 to the price parameter and 3 to the quantity parameter. Inside the function, it calculates 10 * 3 = 30 and prints "Total: $30". When you call it again with calculate_total(5, 7), the parameters receive new values: price = 5 and quantity = 7, so it calculates 5 * 7 = 35 and prints "Total: $35". The same function works with different values because parameters let you pass in different arguments each time!
Step 4: Using Variables as Arguments
You don't have to pass literal values like strings or numbers directly. You can also pass variables as arguments. The variable's value will be passed to the function.
def greet(name):
print("Hello, " + name + "!")
person = "Alice"
greet(person) # Pass the variable
age = 25
def print_age(years):
print("Age: " + str(years))
print_age(age) # Pass the variable
How It Works
When you write greet(person), Python looks up the value of the variable person (which is "Alice") and passes that value to the function. It's the same as writing greet("Alice"). This is useful when you have values stored in variables that you want to use with functions.
Step 5: Common Mistakes
Here are some common mistakes beginners make with parameters and arguments:
# Mistake 1: Forgetting to pass required arguments
def greet(name):
print("Hello, " + name)
greet() # ERROR! Missing argument
# Mistake 2: Passing too many arguments
def greet(name):
print("Hello, " + name)
greet("Alice", "Bob") # ERROR! Too many arguments
# Mistake 3: Wrong argument order
def introduce(name, age):
print(name + " is " + str(age))
introduce(25, "Alice") # Wrong! 25 goes to name, "Alice" to age
Remember: You must pass exactly the right number of arguments, and they must be in the correct order. If a function has 2 parameters, you must pass 2 arguments. If it has 0 parameters, you pass 0 arguments (just empty parentheses).
End-of-Lesson Exercises
Exercise 1: Create a Function with One Parameter
Create a function named say_hello that takes one parameter called name and prints "Hello, [name]!". Then call it with your name.
Use def say_hello(name): followed by a print statement, then call say_hello("YourName").
Exercise 2: Function with Multiple Parameters
Create a function named calculate_area that takes two parameters: length and width. It should calculate and print the area (length × width). Call it with length=5 and width=3.
Your function should multiply length * width and print the result.