🐍 Python for kids · Class 7 · Lesson 1 of 18 · Free
Functions – Build Your Own Tools
⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Define a function with def and trace how Python jumps into it and back.
- Write a docstring that explains what a function does.
- Split a bigger task into small functions that call each other.
A cricket team has a bowler, a batter and a wicket-keeper. Each player has one job and does it well. Big programs work the same way: each function does one job, and together they win the match.
What is a function in Python and why use one?
A function is a named block of code that does one job. You already use built-in ones like print() and len().
Functions save you from copying the same lines again and again. If a line has a bug, you fix it in one place.
They also give a name to an idea. A reader sees print_report() and knows what happens, without reading every line.
▶def school_bell():2 print("Ring ring!")3 print("Time for the next period.")45print("Maths class ends.")6school_bell()7print("Science class ends.")8school_bell()
No variables yet.
▌✏️ Change this code and run it yourself
Maths class ends. Ring ring! Time for the next period. Science class ends. Ring ring! Time for the next period.
💡 Watch the trace. Python jumps into school_bell() at each call, then returns to the next line.
Does Python run a function when it reads def?
No. The def line only teaches Python a new word. The body runs only when you call the function.
So the order of the file matters. A function must be defined before the line that calls it runs.
Below, the def is read first, but nothing prints until the call on the last line.
▶print("Step 1: start of the program")23def show_menu():4 print("1. Idli")5 print("2. Dosa")6 print("3. Vada")78print("Step 2: the function is defined, but not run yet")9show_menu()10print("Step 3: the menu was shown")
No variables yet.
▌✏️ Change this code and run it yourself
Step 1: start of the program Step 2: the function is defined, but not run yet 1. Idli 2. Dosa 3. Vada Step 3: the menu was shown
How do I name a function well?
Use lowercase words joined by underscores, like find_total or print_bill. This style is called snake_case.
Start the name with a verb, because a function does something. calculate_area is clearer than area_thing.
Never use a name that Python already uses, like print or list. You would hide the built-in tool.
-------------------- ANNUAL SPORTS DAY -------------------- -------------------- PRIZE LIST --------------------
💡 print_title() calls print_line() inside it. A function can use other functions, just like you use print().
What is a docstring in a Python function?
A docstring is a string in triple quotes on the first line of a function's body. It explains what the function does.
Python ignores it when running, but people read it. help() also shows it, and so do code editors.
Write it as one short sentence: what goes in and what comes out.
500 Return the number of paise in the given rupees.
💡 __doc__ (two underscores on each side) holds the docstring. Try help(rupees_to_paise) in your own editor too.
How do I break a big task into small functions?
Programmers split a big problem into small steps. Each step becomes one small function.
Here, a canteen bill has three parts: read the order, work out the cost, print the bill.
The last function, make_bill(), only calls the others. It reads almost like a plan written in English.
How many plates of pav bhaji? 3 3 plates x 60 = 180 rupees
What does a function give back if it has no return?
Every function call gives back a value. If the function has no return line, it gives back None.
None is Python's word for "nothing here". Printing a value and returning it are not the same thing.
So if you want to use the answer later, use return, not print().
▶def shout(word):2 print(word.upper())34result = shout("chalo")5print("The function gave back:", result)
No variables yet.
▌✏️ Change this code and run it yourself
CHALO The function gave back: None
🤔 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 hello():
print("Namaste")
print("Start")
hello()
hello()Question 2. What does this print?
def a():
print("A")
b()
def b():
print("B")
a()
b()Question 3. What does this print?
def double(n):
print(n * 2)
x = double(4)
print(x)🧩 Your challenge
Make a report-card printer. Write print_border() to print 25 = signs, and print_card(name, marks) to print the border, the name, the marks out of 100, and the border again. Call print_card() for two students whose names and marks are typed in.
Name: Ananya Marks: 92 ========================= Student: Ananya Marks: 92 / 100 ========================= Name: Kabir Marks: 85 ========================= Student: Kabir Marks: 85 / 100 =========================
⚠️ Common mistakes (and how to fix them)
❌ Wrong
say_hi()
def say_hi():
print("Hi!")NameError: name 'say_hi' is not defined
✅ Right
def say_hi():
print("Hi!")
say_hi()Python reads the file from top to bottom. A function must be defined before the line that calls it.
❌ Wrong
def greet():
print("Good morning!")
greetNo error message – but nothing is printed, because the function was never called.
✅ Right
def greet():
print("Good morning!")
greet()Writing only the name refers to the function. You need the brackets () to actually run it.
❌ Wrong
def area(side):
print(side * side)
total = area(3) + area(4)
print(total)TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
✅ Right
def area(side):
return side * side
total = area(3) + area(4)
print(total)A function without return gives back None, and Python cannot add None + None. Use return to send the answer back.
📌 Remember
- A function is a named block of code that does one job; def creates it.
- The body runs only when you call the function with brackets, like school_bell().
- After a call ends, Python goes back to the line just after the call.
- Use snake_case names that start with a verb, and add a docstring.
- Functions can call other functions, so big tasks become small steps.
- A function with no return gives back None.
Finished the quiz and the challenge?
Questions students ask
What is the difference between defining and calling a function in Python?
Defining with def teaches Python the function's steps but runs nothing. Calling it by name with brackets, like greet(), actually runs those steps.
Can a Python function call another function?
Yes. A function can call any function that exists when the call runs. This is how big programs are built from small parts.
Why does my Python function print None?
Your function has no return line, so it gives back None. If you print the result of the call, you see None. Add return with the value you want.
Is a docstring the same as a comment?
Not quite. A comment starts with # and is thrown away. A docstring is a string stored with the function, so help() can show it.