- Define "parameter" as a variable in a function's definition.
- Define "argument" as the value passed to a function when it is called.
- Write a function that accepts one or more parameters.
- Call a function and pass the required arguments to it.
Giving Your Functions "Ingredients"
The functions we wrote in the last lesson were a bit like a broken record—they did the exact same thing every time. A function that just says "Hello!" is not very useful. A function that can say hello to any specific person is much better. To do this, we need to pass data into the function using parameters.
Key Vocabulary: Parameter vs. Argument
This is one of the most important vocabulary distinctions in all of programming:
- A Parameter is the variable name listed inside the parentheses in the function's definition. It's the "ingredient" the recipe requires.
- An Argument is the actual value or data that you send to the function when you call it. It's the "ingredient" you actually use.
The Syntax
You define parameters by listing them as variable names inside the parentheses. When you call the function, you provide the arguments in the same order.
# 'person_name' is the PARAMETER
def greet(person_name):
# The parameter acts like a local variable inside the function
print(f"Hello, {person_name}! It's nice to meet you.")
# "Alice" and "Bob" are the ARGUMENTS we pass to the function
greet("Alice")
greet("Bob")
Live Example: The Greeter
The Trinket below demonstrates how to use a function with a single parameter and a function with multiple parameters. Run the code, then follow the "Your Turn" instructions printed at the end to experiment!
Parameters are what make functions truly reusable and powerful. Instead of writing the same logic over and over for different data, you can write one flexible function and call it with any data you need. This is a huge step towards writing professional-quality code.
Interactive Sandbox
Use this space to practice writing code and complete the "Your Turn" activities above.