Python Function Arguments
Table of Contents
In the last lesson, we learned about Defining Python Functions. Now letβs pass information into a function so it can work with different values.
βοΈ Letβs Talk About Giving Information to Functions
In the last article, we learned how to define a function. And how to call it. But most of the time, a function needs some information to do its job.
For example, if we have a function that adds two numbers, it needs to know which two numbers to add. That information is called an argument.
Arguments are the values we pass into a function when we call it.
Think of a function like a coffee machine. We put in coffee beans and water. Those are the arguments. The machine uses them to make coffee. Without them, it cannot do anything.
βοΈ Parameters vs Arguments in Python
Before we go further, letβs clear up a common confusion.
People often use the words βparameterβ and βargumentβ interchangeably. But they are slightly different.
A parameter is the variable name we write in the function definition. It is like a placeholder.
An argument is the actual value we pass when we call the function.
Let me show us.
def greet(name): # name is a parameter print(f"Hello, {name}!")
greet("Alex") # "Alex" is an argumentIn the function definition, name is the parameter. In the function call, "Alex" is the argument.
In everyday talk, people mix them up. And that is fine. But it is good to know the difference.
π‘ Positional Arguments in Python
The most common way to pass arguments is by position. We just list them in order. Python matches them to the parameters in order.
def introduce(name, age, city): print(f"My name is {name}. I am {age} years old. I live in {city}.")
introduce("Alex", 25, "London")Output
My name is Alex. I am 25 years old. I live in London.Here, "Alex" goes to name, 25 goes to age, and "London" goes to city. That is because they are in that order.
This is called positional arguments. The position matters.
If we change the order, the values go to different parameters.
introduce("London", "Alex", 25)Output
My name is London. I am Alex years old. I live in 25.That is nonsense. But Python does not know that. It just matches by position. So we have to be careful about the order.
π§ͺ Keyword Arguments in Python
To avoid confusion with order, we can use keyword arguments. We write the parameter name and the value together.
introduce(name="Alex", age=25, city="London")Output
My name is Alex. I am 25 years old. I live in London.Now the order does not matter. We can write them in any order.
introduce(city="London", name="Alex", age=25)Output
My name is Alex. I am 25 years old. I live in London.Same result. Because we used the parameter names. Python matches them by name, not by position.
We will cover keyword arguments in more detail in a later article. But this is a quick preview.
π Mixing Positional and Keyword Arguments
We can mix them. But positional arguments must come first.
introduce("Alex", age=25, city="London")This works. "Alex" is positional and goes to name. The others are keyword and go to their names.
But this does not work:
introduce(name="Alex", 25, "London") # Error!We cannot put positional arguments after keyword arguments. Python will not know what to do.
So letβs remember: positional first, then keyword.
π Python Function Arguments: A Real-Life Example
Letβs write a function that calculates the total price of items.
def calculate_total(price, quantity, tax_rate): subtotal = price * quantity tax = subtotal * tax_rate total = subtotal + tax print(f"Total: ${total:.2f}")
calculate_total(10, 3, 0.1)Output
Total: $33.00Now with keyword arguments:
calculate_total(price=10, quantity=3, tax_rate=0.1)Same output. But now it is clear what each value means.
π Why Arguments Matter in Python
Arguments make functions flexible. Without them, a function would always do the same thing. With them, we can give different inputs and get different outputs.
For example, a function that adds two numbers is useless if it always adds 5 and 3. But if we can pass any two numbers, it becomes useful.
That is the power of arguments.
β οΈ Common Mistakes with Python Function Arguments
β Mistake 1: Wrong Number of Arguments
def add(a, b): print(a + b)
add(5) # Error! Missing one argumentPython will give us an error because we did not provide enough arguments.
So what do we do? We provide the right number.
add(5, 3)π« Mistake 2: Too Many Arguments
add(5, 3, 2) # Error! Too many argumentsAgain, Python expects exactly two arguments. Not more, not less.
So what do we do? We pass exactly what the function needs.
add(5, 3)π Mistake 3: Positional After Keyword
introduce(name="Alex", 25, "London") # Error!So what do we do? We put positional arguments first.
introduce("Alex", 25, "London")Or we use all keyword arguments:
introduce("Alex", age=25, city="London")π Mistake 4: Forgetting to Pass an Argument
def greet(name): print(f"Hello, {name}!")
greet() # Error! Missing argumentSo what do we do? We pass the argument.
greet("Alex")Next, we will learn about Python Return Values and get results back from functions.