TalentJR

🐍 Python for kids · Class 5 · Lesson 13 of 18 · Free

Functions – Make Your Own Commands

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

In this lesson you will

  • Create a function with def and call it by its name.
  • Pass information into a function with parameters.
  • Send an answer back from a function with return.

print(), len() and max() are commands that Python already knows. What if you could make your own command, like greet() or find_grade()? Today you will!

What is a function in Python?

A function is a named block of code that does one job. You write it once and use it many times.

Think of a pressure cooker. You do not build a new one for every meal. You just use it again.

You make a function with def, a name, brackets and a colon. The indented lines are its body.

🎬 How Python thinks — step 1 of 8Python is about to run line 1
▶def say_hello():
2 print("Namaste!")
3 print("Welcome to coding class.")
4
5say_hello()
6say_hello()
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Namaste!
Welcome to coding class.
Namaste!
Welcome to coding class.

💡 Defining a function does not run it. The lines run only when you call it by name, like say_hello().

How do I give a function information with parameters?

A parameter is a variable in the brackets of def. It catches the value you send in.

When you call greet("Riya"), name becomes "Riya" inside the function. Next time it can be someone else.

This makes one function useful for many different values.

🐍 Python — you can edit this code
Output
Happy birthday, Riya!
Happy birthday, Arjun!
Whose birthday is it? Meera
Happy birthday, Meera!

Can a function have more than one parameter?

Yes. Separate the parameters with commas. The values you send are matched in the same order.

Below, the first value goes into length and the second into width.

Swap the order and the meaning can change, so keep the order in mind.

🐍 Python — you can edit this code
Output
A 5 m by 4 m room has area 20 sq m.
A 10 m by 3 m room has area 30 sq m.

What does return do in a Python function?

print() only shows a value on the screen. return sends the value back to the line that called the function.

You can store the returned value in a variable, or use it in a sum. It is like asking a friend a question and writing down the answer.

When Python reaches return, the function ends straight away.

🎬 How Python thinks — step 1 of 8Python is about to run line 1
▶def add_tax(price):
2 return price + price * 5 // 100
3
4bill = add_tax(200)
5print("Bill with tax:", bill)
6print("Two bills:", add_tax(100) + add_tax(300))
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
Bill with tax: 210
Two bills: 420

How do I use if inside a function?

A function body can hold anything you have learned: if, loops, lists.

Each return gives back a different answer. Python leaves the function at the first return it reaches.

This grade function is tested once and can then be trusted for every student.

🎬 How Python thinks — step 1 of 12Python is about to run line 1
▶def find_grade(marks):
2 if marks >= 90:
3 return "A"
4 elif marks >= 75:
5 return "B"
6 elif marks >= 35:
7 return "C"
8 return "Needs help"
9
10print(find_grade(95), find_grade(80), find_grade(20))
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🐍 Python — you can edit this code
Output
A B Needs help

Why are functions so useful?

Functions break a big program into small, named jobs. Each job is easy to read and test.

If you find a bug, you fix it in one place. Every call gets the fix.

Here a function works on a whole list and returns the average.

🐍 Python — you can edit this code
Output
Maths average: 80.0
Science average: 75.0

🤔 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 double(n):
    return n * 2

print(double(4) + double(1))

Question 2. What does this print?

def shout(word):
    print(word.upper())

print("start")
shout("hi")

Question 3. What does this print?

def check(age):
    if age >= 18:
        return "adult"
    return "child"

print(check(18), check(11))

🧩 Your challenge

Write a function called rupees_to_coins(amount) that returns how many 5-rupee coins fit into the amount. Then ask the user for an amount and print the answer using your function.

Expected output (with the inputs given):
How many rupees do you have? 47
You can get 9 five-rupee coins.
🐍 Python — you can edit this code

⚠️ 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 from top to bottom. A function must be defined before the line that calls it.

❌ Wrong

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

total = add(2, 3)
print("Total is", total * 2)

TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

✅ Right

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

total = add(2, 3)
print("Total is", total * 2)

print() only shows the answer. Without return, the function gives back None, which cannot be multiplied.

❌ 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, 4))

The function has two parameters, so every call must send two values.

📌 Remember

  • A function is a named block of code that does one job.
  • def name(): creates a function; name() calls it and runs it.
  • Parameters catch the values you send in, in the same order.
  • return sends an answer back and ends the function at once.
  • print() only shows a value; return lets you store and reuse it.
  • Define a function before the line that calls it.

Finished the quiz and the challenge?

Questions students ask

What is the difference between print and return in Python?

print() shows a value on the screen. return sends the value back to your program. Then you can store it or use it in a sum.

What is a parameter in a Python function?

A parameter is a variable in the brackets after def. It holds the value that you pass when you call the function.

Can a function return nothing?

Yes. A function without return gives back None. That is fine for functions that only print, like greet().

Why should I use functions?

Functions save typing, keep code tidy, and let you fix a bug in one place. Big apps are built from many small functions.