TalentJR

🐍 Python for kids · Class 6 · Lesson 16 of 18 · Free

Functions – Build Your Own Tools

⏱ About 35 minutes · ▶ run real Python here — nothing to install · no sign-in

In this lesson you will

  • Define functions with parameters and send results back with return.
  • Give parameters default values and call functions with keyword arguments.
  • Explain why variables inside a function stay inside it.

A mobile phone has a calculator app, a camera app and a torch app. Each app does one job, and you open it whenever you need it. Functions are the apps inside your program. Today you will build your own.

What is a function and why do programmers use them?

A function is a named block of code that does one job. You define it once with def and call it many times.

Without functions, you would copy the same lines again and again. One bug would then need fixing in many places.

Defining a function only teaches Python the recipe. The code runs when you call the function by its name.

🎬 How Python thinks — step 1 of 10Python is about to run line 1
▶def draw_line():
2 print("=" * 20)
3
4draw_line()
5print(" REPORT CARD")
6draw_line()
7print("Maths: 88")
8draw_line()
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
====================
  REPORT CARD
====================
Maths: 88
====================

How do parameters and arguments work?

A parameter is a name inside the brackets of def. An argument is the real value you pass when you call.

Python matches arguments to parameters by position: first to first, second to second.

So in make_bill("pen", 10, 3), item is "pen", price is 10 and qty is 3.

🐍 Python — you can edit this code
Output
3 x pen @ Rs 10 = Rs 30
2 x notebook @ Rs 45 = Rs 90

What is the difference between return and print?

print() shows a value on the screen, and that is all. The rest of the program cannot use it.

return hands a value back to the caller. You can store it, add it, or pass it to another function.

A function with no return gives back a special value called None. That is why the last line prints None.

🎬 How Python thinks — step 1 of 10Python is about to run line 1
▶def square_print(n):
2 print(n * n)
3
4def square_return(n):
5 return n * n
6
7result = square_return(4) + square_return(3)
8print("Sum of squares:", result)
9answer = square_print(5)
10print("answer is", answer)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Sum of squares: 25
25
answer is None

💡 Use return when other code needs the answer. Use print only to show something to the user.

How do I give a parameter a default value?

A default value is used when the caller does not send that argument. Write it with = in the def line.

Here the tip is 10 percent unless you say otherwise. Most calls stay short, but you can still change it.

You can also name the argument when calling, like tip=5. This is called a keyword argument and makes calls easy to read.

🐍 Python — you can edit this code
Output
550
600
840

💡 Parameters with defaults must come after parameters without defaults.

Why can't I use a function's variable outside it?

Variables made inside a function are local. They exist only while the function runs, then they vanish.

This is a good thing. Two functions can both use a name like total without disturbing each other.

To get a value out, return it. Do not try to reach inside the function.

🎬 How Python thinks — step 1 of 8Python is about to run line 1
▶def add_marks(a, b):
2 total = a + b
3 return total
4
5total = 0
6result = add_marks(40, 45)
7print("result:", result)
8print("outside total is still:", total)
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
result: 85
outside total is still: 0

Can one function call another function?

Yes. Big programs are built from small functions that call each other, like Lego blocks.

Below, grade() turns a percentage into a grade, and report() uses both percent() and grade().

Each function is short and easy to test. Together they do a bigger job.

🐍 Python — you can edit this code
Output
Riya: 92.0% grade A
Kabir: 70.0% grade B

💡 :.1f inside the f-string shows the number with one digit after the decimal point.

🤔 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 triple(x):
    return x * 3

print(triple(triple(2)))

Question 2. What does this print?

def greet(name, msg="Hello"):
    return msg + ", " + name

print(greet("Asha"))
print(greet("Asha", "Namaste"))

Question 3. What does this print?

def show(n):
    print(n + 1)

x = show(4)
print(x)

🧩 Your challenge

Write a function fuel_cost(km, mileage=15, price=100) that returns the rupees of petrol needed for a trip. Petrol needed = km / mileage litres. Call it for a 300 km trip with the defaults, then for 300 km with mileage 20. Print both answers.

Expected output :
Normal car: Rs 2000.0
Better mileage: Rs 1500.0
🐍 Python — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

greet("Riya")

def greet(name):
    print("Hi", name)

NameError: name 'greet' is not defined

✅ Right

def greet(name):
    print("Hi", name)

greet("Riya")

Python reads from top to bottom. You must define a function before the line that calls it.

❌ Wrong

def area(l, w):
    a = l * w

area(5, 4)
print(a)

NameError: name 'a' is not defined

✅ Right

def area(l, w):
    a = l * w
    return a

a = area(5, 4)
print(a)

a is local to the function and vanishes when it ends. Return the value and store it outside.

❌ Wrong

def add(a, b):
    return a + b

print(add(5))

TypeError: add() missing 1 required positional argument: 'b'

✅ Right

def add(a, b):
    return a + b

print(add(5, 3))

add needs two arguments but got one. Send both, or give b a default value like b=0.

📌 Remember

  • def name(parameters): creates a function; calling it runs the body.
  • Arguments are matched to parameters by position, or by name with keyword arguments.
  • return sends a value back; a function without return gives None.
  • Default values like tip=10 are used when the caller leaves that argument out.
  • Variables inside a function are local and disappear when it ends.
  • Small functions can call each other to build bigger programs.

Finished the quiz and the challenge?

Questions students ask

What is the difference between a parameter and an argument?

The parameter is the name in the def line. The argument is the actual value you pass when calling.

Can a Python function return more than one value?

Yes, write return a, b. You get both back and can store them with x, y = my_function().

Why does my function print None?

Your function has no return, so it gives back None. If you print the call, None appears.

How should I name my functions?

Use small letters and underscores, and start with a verb, like find_total or show_menu.