🐍 Python for kids · Class 5 · Lesson 14 of 18 · Free
Advanced Functions – Defaults, Keywords & Scope
⏱ About 25 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Give a parameter a default value that is used when nothing is passed.
- Call a function with keyword arguments and return two values at once.
- Explain the difference between a local and a global variable.
Your mother packs your tiffin every day. Most days it is roti and sabzi, unless you ask for something else. Python functions can work the same way: they use a usual choice, unless you give a new one.
What is a default parameter in Python?
In Lesson 13, every parameter needed a value when you called the function. Forget one, and Python shows an error.
A default parameter has a ready value, written with = in the def line. If you do not pass that value, Python uses the default.
If you do pass a value, your value wins. The default is only a backup.
▶def greet(name, greeting="Namaste"):2 print(f"{greeting}, {name}!")34greet("Riya")5greet("Arjun", "Good morning")
No variables yet.
▌✏️ Change this code and run it yourself
Namaste, Riya! Good morning, Arjun!
💡 Parameters with a default must come after the parameters without one.
What are keyword arguments?
Normally Python matches values to parameters by position: first value to first parameter, and so on.
With keyword arguments, you write the parameter name, an = sign, and the value. Now the order does not matter.
Keywords also make a call easier to read. Anyone can see which value is the sweet and which is the drink.
Main: idli | Sweet: laddoo | Drink: water Main: poha | Sweet: jalebi | Drink: water Main: paratha | Sweet: kheer | Drink: lassi
Can a function return more than one value?
Yes! Write the values after return, separated by commas. Python packs them together and sends them back.
When you call the function, put the same number of variable names on the left side. Each variable catches one value, in order.
This is useful when one job gives two answers, like a total and an average.
▶def total_and_average(a, b, c):2 total = a + b + c3 average = total / 34 return total, average56t, avg = total_and_average(78, 85, 92)7print("Total marks:", t)8print("Average:", round(avg, 1))
No variables yet.
▌✏️ Change this code and run it yourself
Total marks: 255 Average: 85.0
What is a local variable in Python?
A variable made inside a function is a local variable. It lives only while the function is running.
When the function ends, its local variables are thrown away. Code outside the function cannot see them.
Think of it like rough work on a slate. Once the sum is done, the slate is wiped clean. Only the returned answer is kept.
▶def match_score(fours, sixes):2 runs = fours * 4 + sixes * 63 print("Inside the function, runs =", runs)4 return runs56score = match_score(5, 2)7print("Outside, the score is", score)
No variables yet.
▌✏️ Change this code and run it yourself
Inside the function, runs = 32 Outside, the score is 32
💡 Outside the function, runs does not exist. That is why we catch the answer in score.
What is a global variable?
A variable made outside all functions is a global variable. Every function in the program can read it.
Globals are handy for fixed values that never change, like a school name or a ticket price.
Does a function need to change a value? Then pass the value in, and return the new one. That keeps each function clean and easy to check.
Meera | Class 5 | Sunrise Public School Kabir | Class 5 | Sunrise Public School
💡 Programmers often write fixed globals in CAPITAL letters, so they stand out.
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
def power(base, exp=2):
return base ** exp
print(power(3))
print(power(2, 3))Question 2. What does this print?
def show(a, b):
print(a, b)
show(b="bat", a="ball")Question 3. What does this print?
def min_max(nums):
return min(nums), max(nums)
low, high = min_max([7, 2, 9, 4])
print(high - low)🧩 Your challenge
Write a function bill(item, price, quantity=1) that returns two values: the total cost and a message. Call it once with a quantity and once without, and print both results.
3 x samosa = 45 rupees 1 x chai = 10 rupees
⚠️ Common mistakes (and how to fix them)
❌ Wrong
def greet(greeting="Hello", name):
print(greeting, name)
greet("Hi", "Asha")SyntaxError: parameter without a default follows parameter with a default
✅ Right
def greet(name, greeting="Hello"):
print(greeting, name)
greet("Asha", "Hi")Parameters with a default must come last, or Python cannot tell which value goes where.
❌ Wrong
def add_runs(a, b):
total = a + b
return total
add_runs(40, 25)
print(total)NameError: name 'total' is not defined
✅ Right
def add_runs(a, b):
total = a + b
return total
total = add_runs(40, 25)
print(total)total is local to the function, so catch the returned value in a variable outside.
❌ Wrong
def tiffin(main, sweet):
print(main, sweet)
tiffin(main="poha", "jalebi")SyntaxError: positional argument follows keyword argument
✅ Right
def tiffin(main, sweet):
print(main, sweet)
tiffin("poha", sweet="jalebi")Positional values must come before keyword values in a function call.
📌 Remember
- A default parameter (name=value) is used when you do not pass that value.
- Parameters with defaults must come after the ones without defaults.
- Keyword arguments (sweet="jalebi") match by name, so their order does not matter.
- return a, b sends back two values; catch them with x, y = function().
- Local variables live only inside their function; global variables can be read everywhere.
- Pass values in and return answers out, instead of changing globals.
Finished the quiz and the challenge?
Questions students ask
What is the difference between a parameter and an argument?
A parameter is the name in the def line, like name. An argument is the actual value you pass when you call, like "Riya".
Can a function have more than one default parameter?
Yes. You can give defaults to as many parameters as you like, as long as they all come after the parameters without defaults.
Why can't I use a variable from inside a function outside it?
It is a local variable, so it disappears when the function ends. Use return to send its value out, and store it in a new variable.
Should I use global variables?
Reading a fixed global, like a school name, is fine. For values that change, pass them as arguments and return the result. Your code stays easier to understand.