๐ Python for kids ยท Class 7 ยท Lesson 2 of 18 ยท Free
Parameters & Return Values
โฑ About 30 minutes ยท โถ run real Python here โ nothing to install ยท no sign-in
In this lesson you will
- Pass values into a function by position, by keyword and with default values.
- Send results back with return, including two values at once.
- Explain why a variable made inside a function cannot be seen outside it.
A juice stall makes mango, orange or mosambi juice with the same machine. You choose the fruit, and the machine hands back a glass. Parameters are the fruit you put in. return is the glass that comes out.
What is the difference between a parameter and an argument?
A parameter is the name inside the brackets of def. An argument is the actual value you send in a call.
In discount(500, 10), the arguments 500 and 10 go into the parameters price and percent, in that order.
Inside the function, parameters work like normal variables. They get fresh values on every call.
โถdef discount(price, percent):2 cut = price * percent // 1003 return price - cut45bag = discount(500, 10)6shoes = discount(1200, 25)7print("Bag:", bag)8print("Shoes:", shoes)
No variables yet.
โโ๏ธ Change this code and run it yourself
Bag: 450 Shoes: 900
How do default parameter values work in Python?
You can give a parameter a default value with =. If the caller leaves it out, Python uses the default.
This is handy when one value is usually the same. Most bus tickets are for one person, so people = 1.
Parameters with defaults must come after the ones without defaults.
โถdef ticket(place, people=1):2 print(f"{people} ticket(s) to {place}")34ticket("Pune")5ticket("Nashik", 4)
No variables yet.
โโ๏ธ Change this code and run it yourself
1 ticket(s) to Pune 4 ticket(s) to Nashik
What are keyword arguments in Python?
Normally, arguments are matched by position: first to first, second to second.
With keyword arguments you write the parameter's name, like percent=20. Then the order does not matter.
This makes calls easier to read, especially when a function has many parameters. print() uses this for sep and end.
2 x medium pizza 3 x medium lassi 1 x large pizza A-B-C
Why use return instead of print in a function?
print() only shows a value on the screen. The program cannot use that value again.
return hands the value back to the caller. You can store it, add to it, or pass it to another function.
return also ends the function at once. Any lines after it inside the function are skipped.
95 -> A 80 -> B 60 -> C
๐ก Once a return runs, the function stops. So 95 never reaches the check for 75.
Can a Python function return two values?
Yes. Write the values after return with a comma between them. Python packs them together.
On the calling side, put two variable names before =. Python unpacks the values into them, in order.
Below, one function gives back both the quotient and the remainder of sharing laddoos.
Keep the order the same on both sides. If you swap a and b, the answers get mixed up.
โถdef share(laddoos, kids):2 each = laddoos // kids3 left = laddoos % kids4 return each, left56a, b = share(17, 5)7print("Each child gets", a)8print("Left in the box:", b)
No variables yet.
โโ๏ธ Change this code and run it yourself
Each child gets 3 Left in the box: 2
What is a local variable in a Python function?
A variable created inside a function is local. It lives only while the function runs, then it is thrown away.
Code outside the function cannot see it. That keeps functions from messing with each other's variables.
If you need a value outside, return it. Do not try to reach inside the function.
Think of a function as an exam hall. Rough work stays inside; only the answer sheet comes out.
Final score: 45 Is 'bonus' known outside? False
๐ก globals() lists the names known outside all functions. bonus is not one of them.
๐ค 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), power(2, 3))Question 2. What does this print?
def check(n):
if n > 10:
return "big"
print("checking")
return "small"
print(check(50))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(amount, tip_percent=10) that returns two values: the tip and the total (amount + tip). Use whole-rupee maths with //. Read an amount from the user, call bill() once with the default tip and once with tip_percent=15, and print both results.
Food bill in rupees: 840 10% tip: 84, total: 924 15% tip: 126, total: 966
โ ๏ธ Common mistakes (and how to fix them)
โ Wrong
def ticket(people=1, place):
print(people, place)
ticket("Goa")SyntaxError: parameter without a default follows parameter with a default
โ Right
def ticket(place, people=1):
print(people, place)
ticket("Goa")Parameters with default values must come after all the parameters without defaults.
โ Wrong
def total_marks(a, b, c):
total = a + b + c
total_marks(80, 75, 90)
print(total)NameError: name 'total' is not defined
โ Right
def total_marks(a, b, c):
total = a + b + c
return total
total = total_marks(80, 75, 90)
print(total)total is a local variable, so it vanishes when the function ends. Return it and store the result outside.
โ Wrong
def area(length, width):
return length * width
print(area(5))TypeError: area() missing 1 required positional argument: 'width'
โ Right
def area(length, width):
return length * width
print(area(5, 3))Every parameter without a default needs an argument. Send both values, or give width a default.
๐ Remember
- Parameters are the names in def; arguments are the values you send in a call.
- A default value, like people=1, is used when the caller leaves that argument out.
- Keyword arguments, like size="large", match by name, so their order does not matter.
- return sends a value back and ends the function at once.
- return a, b sends two values; unpack them with x, y = f().
- Variables made inside a function are local and disappear when it ends.
Finished the quiz and the challenge?
Questions students ask
What is the difference between parameters and arguments in Python?
Parameters are the variable names written in the def line. Arguments are the real values you pass when you call the function.
Can a Python function have no parameters?
Yes. Write empty brackets, like def show_menu():. You still call it with empty brackets: show_menu().
What happens to code after return in a function?
It does not run. return ends the function straight away, so put return at the point where the answer is ready.
How do I use a variable from inside a function outside it?
Return the value from the function. Then store it where you call it, like total = add(2, 3).