Python Function Arguments

In the last lesson you learned how to package code into a reusable block in Defining Functions. But those functions always did the exact same thing every time you called them. A real function usually needs some information to work with. A greeting function needs to know who to greet. An adding function needs to know which two numbers to add. So how do you hand that information to a function? That is what arguments are for.

πŸ€” Why Pass Information Into a Function?

Picture a function called greet that prints β€œHello, Alex!”. Nice. But it can only ever say hello to Alex. The moment Riya shows up, you are stuck writing a whole new function for her. Then another one for Arjun. That gets silly fast.

The fix is to leave a blank spot in the function. You say β€œgreet somebody” and you fill in the name later when you call it. That blank spot is how you pass information in. The value you put there is called an argument.

🧩 Parameter vs Argument (the part everyone mixes up)

These two words sound the same and people use them loosely. But there is a clean and simple difference between them. Once you know it, everything else clicks.

A parameter is the name you write in the function definition. It is the blank spot, the placeholder.

An argument is the real value you pass in when you actually call the function.

Here is the same idea side by side.

Term Where it lives What it is
Parameter In the def line The placeholder name, like name
Argument In the call The real value, like "Alex"

Think of it like a form at a hotel. The form has a printed line that says β€œName: ______”. That printed label is the parameter. The actual name you write on the line, β€œAlex”, is the argument. The form is reusable. Anyone can write their own name on it.

Tip

A quick memory trick. Parameter goes with the function Plan, which is the definition. Argument goes with the Actual call. Mixing up the words is not a big deal in everyday talk. But knowing which is which helps a lot when you read error messages.

πŸ‘‹ A Function That Greets a Given Person

Let us turn that one-name greet into something that works for anybody. We add a parameter inside the parentheses.

This function takes one parameter called name and prints a hello message using it.

def greet(name):
print(f"Hello, {name}!")
greet("Alex")
greet("Riya")
greet("Arjun")

When you run it, you get:

Output

Hello, Alex!
Hello, Riya!
Hello, Arjun!

Now walk through what happened, line by line.

  • def greet(name): defines the function. The name inside the parentheses is the parameter. It is just a placeholder waiting for a value.
  • print(f"Hello, {name}!") is the body. The f before the string makes it an f-string, so {name} gets replaced by whatever value name holds.
  • greet("Alex") calls the function and passes "Alex" as the argument. Inside the function, name becomes "Alex", so it prints β€œHello, Alex!”.
  • The next two calls do the same with "Riya" and "Arjun".

See the benefit here? One function, three different greetings, and zero repeated code. The parameter is what makes the function flexible.

βž• A Function With Two Arguments

A lot of functions need more than one piece of information. To add two numbers, the function has to know both of them. So we give it two parameters, separated by a comma.

This function takes two numbers, adds them, and prints the total.

def add(a, b):
total = a + b
print(f"{a} + {b} = {total}")
add(3, 5)
add(10, 20)

Running it prints:

Output

3 + 5 = 8
10 + 20 = 30

Here is what each part does.

  • def add(a, b): sets up two parameters, a and b. The comma separates them.
  • total = a + b adds the two values and stores the result in a variable called total.
  • print(...) shows the whole sum in a readable line.
  • add(3, 5) passes 3 as the first argument and 5 as the second. So a becomes 3 and b becomes 5.

Here is the key thing to notice. The first argument fills the first parameter. The second argument fills the second parameter. Order matters. That brings us to the next idea.

πŸ”’ Positional Arguments: Order Matters

The arguments we have used so far are called positional arguments. The name says it all. Python matches each value to a parameter by its position, left to right. The first value goes to the first parameter. The second value goes to the second parameter. And so on.

This usually does exactly what you expect. But it also means that if you swap the order, you change the meaning.

Look at this function that introduces a person and their city. The order of the two arguments decides which is which.

def introduce(person, city):
print(f"{person} lives in {city}.")
introduce("Alex", "Berlin")
introduce("Berlin", "Alex")

It prints:

Output

Alex lives in Berlin.
Berlin lives in Alex.

The second call is nonsense, but Python does not complain. It just fills the parameters in the order you gave them. So person got "Berlin" and city got "Alex". The computer did exactly what you told it, not what you meant.

So with positional arguments, you are in charge of getting the order right. With add(a, b) it does not matter much, because addition is the same both ways. But with most functions it matters a lot.

Note

The number of arguments has to match the number of parameters too. If greet expects one name and you give it zero or two, Python stops and shows an error. We will look at that next.

⚠️ Common Mistakes

A few slip-ups come up again and again when people first start passing arguments. Here is what to watch for.

  • Passing the wrong number of arguments. If the function asks for two values and you give one, Python raises a TypeError.
def add(a, b):
print(a + b)
# ❌ Avoid: only one argument when two are needed
add(3)

Output

TypeError: add() missing 1 required positional argument: 'b'
# βœ… Good: give both values
add(3, 5)
  • Getting the order wrong. Positional arguments go by position. Swapping them silently changes the meaning, as we saw with introduce.
def book_ticket(name, seat):
print(f"{name} booked seat {seat}.")
# ❌ Avoid: arguments in the wrong order
book_ticket("12A", "Riya") # prints: 12A booked seat Riya.
# βœ… Good: name first, seat second
book_ticket("Riya", "12A") # prints: Riya booked seat 12A.
  • Confusing the parameter name with a real value. The parameter is only a placeholder. Writing greet(name) outside the function, where no name variable exists, gives a NameError. You pass a real value like greet("Alex"), not the placeholder word.

βœ… Best Practices

Small habits that keep your functions easy to read and use.

  • Give parameters clear, meaningful names. greet(name) reads better than greet(x).
  • Keep the number of parameters small. If a function needs many inputs, that is often a sign it is trying to do too much.
  • Match the order of your arguments to a sensible real-world order. Name then seat, person then city. It makes the calls read like a sentence.
  • Use plain snake_case for parameter names, the same style you use for variables.

🧩 What You’ve Learned

βœ… A function can take inputs so it does different work each time you call it.

βœ… A parameter is the placeholder name in the def line; an argument is the real value you pass in the call.

βœ… You write parameters inside the parentheses, separated by commas, and you pass matching arguments the same way.

βœ… Positional arguments are matched by order, left to right, so the order you pass them in matters.

βœ… The number of arguments must match the number of parameters, or Python raises a TypeError.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    In `def greet(name):`, what is `name` called?

    Why: The placeholder name written in the function definition is a parameter; the real value passed in the call is the argument.

  2. 2

    What does `greet("Riya")` pass into the function?

    Why: "Riya" is the actual value passed at call time, which makes it the argument.

  3. 3

    Given `def introduce(person, city):`, what does `introduce("Berlin", "Alex")` print?

    Why: Positional arguments fill parameters by order, so person becomes "Berlin" and city becomes "Alex".

  4. 4

    What happens if you call `add(3)` when the function is `def add(a, b):`?

    Why: The number of arguments must match the parameters, so a missing required argument raises a TypeError.

πŸš€ What’s Next?

Right now your functions can take information in and print something. But they cannot hand a result back to you to use later. That is the next piece.

Return Values

Share & Connect