Learning Goals
3 minBy the end of this lesson you can:
- Add a parameter to a function with
def greet(name):and use the name inside the body. - Pass an argument when you call:
greet("Aisyah")vsgreet("Faiz"), and see the function behave differently. - Tell the difference between a parameter (in the
def) and an argument (in the call) without flinching.
Warm-Up
5 minYou've already been passing inputs to functions for ages, you just haven't noticed. Look:
print("Hello, world!") len("Selamat datang") sorted("nasi")
print, len, sorted — all functions, all of them take an input inside the parentheses. The input you put inside is called an argument. The fact that print behaves differently each call (printing different text) is exactly because you handed it a different argument.
Today you learn how to build a function of your own that accepts an argument the same way.
Parameter = the slot in the def line. Argument = the actual value you pass when you call. A function with one parameter expects one argument. A handy mnemonic: Parameter is in the Plan; Argument is in the Action.
New Concept · A Slot for the Caller
14 minDefine a function with a parameter
Put a name inside the parentheses of the def line. That name becomes a temporary variable that exists only inside the function body:
def greet(name): print("Hello,", name + "!")
Now greet is a recipe with a blank: "print Hello, then whatever you handed me, then a !". The blank is called name — that's the parameter.
Pass an argument when you call
To fill in the blank, put a value inside the parentheses of the call:
greet("Aisyah") greet("Faiz") greet("Mei")
Hello, Aisyah! Hello, Faiz! Hello, Mei!
Three calls, three different arguments, three different greetings. The function body is written exactly once, but it produces different output for every call.
Inside the function, the parameter is just a variable
You can use the parameter like any other variable — store it, slice it, chain methods on it (callback from PY-L1-23 and PY-L1-24):
def shout_initial(name): first_letter = name[0].upper() print("Your initial is", first_letter) shout_initial("aisyah") shout_initial("faiz")
Your initial is A Your initial is F
Forgetting the argument — TypeError
If the function expects an argument and you forget to pass one, Python tells you off:
def greet(name): print("Hello,", name) greet() # ← oops, no argument
TypeError: greet() missing 1 required positional argument: 'name'
Read the error: "missing 1 required positional argument: 'name'". Python is even telling you which parameter you forgot. Fix it by passing a value inside the parentheses.
The parameter only exists inside the function
This is the most common confusion. name in the function body is not the same as a name variable in the main program. The function's parameter is born when the call starts and dies when the call ends:
def greet(name): print("Hello,", name) greet("Aisyah") print(name) # ← NameError. There is no 'name' out here.
The fact that the slot is called name is a private detail of the function. From the outside, all that matters is "greet takes one argument".
Parameter goes in the def. Argument goes in the call. The parameter is a local variable that exists only while the function is running.
Worked Example · Personalised Mamak Greeter
12 minStep 1 · Add a slot
Take last lesson's welcome_customer and give it a name parameter:
def welcome_customer(name): print("=" * 35) print("Selamat datang,", name + "!") print("=" * 35)
Step 2 · Call it with different arguments
welcome_customer("Aisyah") welcome_customer("Faiz") welcome_customer("Mei")
=================================== Selamat datang, Aisyah! =================================== =================================== Selamat datang, Faiz! =================================== =================================== Selamat datang, Mei! ===================================
Step 3 · Combine with input()
Arguments don't have to be hard-coded strings. They can be anything that evaluates to a value — including the result of input():
Code
# cafe.py — personalised greeter def welcome_customer(name): print("=" * 35) print("Selamat datang,", name + "!") print("=" * 35) print("Tonight's menu: roti canai, nasi lemak, teh tarik.") print() # main program first = input("First customer's name: ").strip() welcome_customer(first) order1 = input("Order: ") second = input("Second customer's name: ").strip() welcome_customer(second) order2 = input("Order: ") print() print(first, "ordered", order1) print(second, "ordered", order2)
Sample run
First customer's name: Aisyah =================================== Selamat datang, Aisyah! =================================== Tonight's menu: roti canai, nasi lemak, teh tarik. Order: roti canai Second customer's name: Faiz =================================== Selamat datang, Faiz! =================================== Tonight's menu: roti canai, nasi lemak, teh tarik. Order: nasi lemak Aisyah ordered roti canai Faiz ordered nasi lemak
One function, one body, two completely different welcome screens. And the main program at the bottom still reads top-to-bottom like a story.
Inside the function, the parameter is name. Outside, we stored the input in first or second. That's fine — Python doesn't care what the outside variable is called. When you call welcome_customer(first), Python copies the value of first into the parameter slot named name.
Try It Yourself
14 minThree tasks. Define each function once; call it with several different arguments.
Write a function greet(name) that prints Hi, <name>! Nice to meet you.. Call it three times with three different names.
Hint
def greet(name): print("Hi,", name + "! Nice to meet you.") greet("Aisyah") greet("Faiz") greet("Mei")
Expected output is three different greetings. If you got TypeError: greet() missing 1 required positional argument: 'name', you forgot to put a string inside the parentheses on a call.
Write a function print_squared(n) that prints <n> squared is <n*n>. Call it with 3, 7, and 12. (Yes, numbers can be arguments too — not just strings.)
Hint
def print_squared(n): print(n, "squared is", n * n) print_squared(3) print_squared(7) print_squared(12)
Expected: 3 squared is 9, 7 squared is 49, 12 squared is 144. The parameter n is a regular variable inside the function — you can do maths with it like any other.
Write a function echo(word) that prints the word three times on three separate lines. Then ask the user for a word with input() and pass their answer to echo. Don't hard-code the word inside the function.
Hint
def echo(word): print(word) print(word) print(word) raw = input("Type a word: ") echo(raw)
If the user types boba, the output should be boba three times on three lines. If you copy-pasted print("boba") inside the function, change it to print(word) — the whole point is that the function works for any word the caller hands in.
Mini-Challenge · Lina's Stuck Greeting
8 minLina wrote a function that's supposed to greet different people. Instead, every call greets Aisyah — even when she passes a different name. Read the script, spot the two bugs, and fix them.
# lina_greeter.py — always greets Aisyah
def greet():
name = "Aisyah"
print("Hello,", name + "!")
greet("Faiz")
greet("Mei")Test it. The first call crashes with TypeError. Add def greet(): back to match (no parameter) and now both calls greet Aisyah no matter what you pass.
- Bug 1. Lina forgot to put a parameter in the
defline. There's no slot for the caller's value to land in. - Bug 2. Inside the function she hard-coded
name = "Aisyah", which would overwrite the parameter even if she had one. Delete that line.
Show one possible fix
# lina_greeter.py — fixed def greet(name): print("Hello,", name + "!") greet("Faiz") greet("Mei")
The classic two-bug combo: no parameter on the def, and a hard-coded value inside the body. Either bug on its own would still misbehave — Bug 1 alone gives a TypeError; Bug 2 alone would silently ignore the caller. Together they hide each other.
Recap
3 minA function becomes truly useful when it can accept input from the caller. The parameter is the named slot in the def line — def greet(name):. The argument is the value you pass at the call site — greet("Aisyah"). Inside the body, the parameter behaves like a normal variable, but it only exists for the duration of that one call. Forget the argument and Python raises TypeError with a helpful message. Next lesson we'll add a second parameter — and a way to give it a sensible default value when the caller doesn't feel like passing one.
Vocabulary Card
- parameter
- The named slot inside the parentheses of the
defline. Acts like a local variable inside the function body. - argument
- The actual value you pass when you call the function.
greet("Aisyah")—"Aisyah"is the argument. - positional argument
- An argument passed in by position (first goes to first parameter, second to second, and so on). The kind you've been writing.
- TypeError (missing argument)
- The error Python raises when you call a function with too few arguments. The message always names the missing parameter.
- local variable
- A variable that lives only inside one function call. Parameters are local. You can't see them from outside the function.
Homework
4 minCreate a new file name_card.py. Build a tiny name-card printer driven by two functions.
- Define
print_card(name)— prints a three-line card: a top border of 25 dashes, the line| Welcome, <name>!, and a bottom border of 25 dashes. - Define
print_initial(name)— printsInitial: <first letter of name in uppercase>. Use slicing/methods from PY-L1-23 and PY-L1-24. - Ask the user for a name with
input("Name: ").strip(). - Call
print_cardwith their name. Then callprint_initialwith their name. - Bonus: call
print_carda second time with the literal string"Stranger". Notice the same function happily prints a different card.
Bring name_card.py next class — in PY-L1-28 we'll add a second parameter so a function can take a name and a greeting language, with sensible defaults.
Sample · name_card.py
# name_card.py — one function, many cards def print_card(name): print("-" * 25) print("| Welcome,", name + "!") print("-" * 25) def print_initial(name): print("Initial:", name[0].upper()) # main program who = input("Name: ").strip() print_card(who) print_initial(who) print_card("Stranger")
The headline trick: print_card(who) and print_card("Stranger") are both valid — the argument can be a variable or a literal. Inside the function, both arrive in the slot named name. And notice we never repeat the row-of-dashes anywhere outside the function.