Introduction:
In Python, functions are a powerful way to organise and reuse code. They become even more useful when we can pass values to them, allowing the same function to work with different data. These values are passed into the function using parameters and arguments.
What is a Parameter?
A parameter is a placeholder or variable that is defined inside the brackets (parentheses) of a function. It represents the data that the function expects to receive when it is called. Think of parameters as containers that hold values passed into the function.
For example:
def greet(name): print(f"Hello, {name}!")
In this function, name
is a parameter. It doesn’t have a value yet—it’s just a placeholder that will be filled in when the function is called.
What is an Argument?
An argument is the actual value that you pass to the function when you call it. When the function is called, the argument is assigned to the parameter, and the function uses that value to perform its task.
For example:
greet("Alice")
Here, "Alice"
is the argument. When we call greet("Alice")
, the value "Alice"
is passed into the function, and the parameter name
is replaced with the argument "Alice"
. This is why the function prints: Hello, Alice!
The Flow of Data In and Out of Functions
The flow of data in and out of a function works like this:
- Input: When the function is called, the argument is passed into the function’s parameter.
- Processing: The function performs its task, using the value of the parameter.
- Output (optional): If the function returns a result, it will send the data back to where the function was called.
For example:
def add_numbers(a, b): return a + b
In this case, a
and b
are parameters that will receive values when the function is called. The function will add those two numbers and return the result.
result = add_numbers(3, 5) print(result) # Output: 8
Here, 3
and 5
are arguments that are passed into the parameters a
and b
. The function then processes the data (adds the two numbers) and returns the result, which is stored in the variable result
and then printed as 8
.
By understanding the difference between parameters (placeholders) and arguments (actual values), you’ll be able to create flexible functions that can handle different inputs and return useful results. This flow of data—arguments in, results out—is key to using functions effectively in programming!
Predict:
Look at the following code. Can you predict what will be printed when the function is called with the given arguments?
def greet(name): print(f"Hello, {name}!") greet("Alice")
What will be printed when greet("Alice")
is called?
Run:
Let’s run a simple example where a function takes two parameters and prints a message based on the arguments passed.
Notice how the output changes depending on the values (arguments) we pass to the function.
Investigate:
Let’s investigate what happens if we pass different arguments to a function that multiplies two numbers. Try changing the values and observe how the function behaves.
What happens when you change the arguments? Try using different numbers and see how the result changes.
Modify:
Now let’s modify this code. Below is a function that adds two numbers, but it only adds 3
and 5
. Modify the function so that it can take any two numbers as parameters and return their sum.
Hint: Add parameters to the function definition and modify the body to add the two parameters.