Lesson 5 of 13

Functions: Your Own Code Recipes! 🍪

Write instructions once, give them a name, and reuse them anytime. A function is your personal mini-machine — built by you!

🧁 Input:
“chocolate”
⚙️ makeCake()
🎂 Output:
“Cake ready!”
🤔 What is a Function?

Write Once. Use Forever!

No more copying the same code over and over — functions do it for you!

When I first learned about functions, I thought they were complicated. Then my teacher said — “Think of a recipe card.” You write the steps once. Then anyone can follow it anytime! That’s exactly what a function does in code. 😊

😰 Without Functions
print("Hello, Ali!")
print("Welcome!")
print("Have fun!")

print("Hello, Sara!")
print("Welcome!")
print("Have fun!")
Repeating yourself — messy!
😎 With a Function!
def greet(name):
  print("Hello,", name)
  print("Welcome!")
  print("Have fun!")

greet("Ali")
greet("Sara")
Clean, short, reusable! 🎉

⚙️ How Functions Work

2 Simple Steps!

Define it once — call it as many times as you want!

1 📝

Step 1 — Define the Function

Write the instructions and give them a name. Think of it like writing a recipe on a card and putting it in your recipe box.

# Define your function
def sayHello():
    print("Hello there!")
    print("Nice to meet you!")
2 🚀

Step 2 — Call the Function

Just write its name wherever you want it to run. One line of code — all the instructions run instantly!

# Call it anytime!
sayHello()
sayHello()
sayHello()

# Output:
"Hello there!" ← x3!

🎮 Try It Yourself!

Interactive Function Machine!

Pick a function, type an input, press Run — see what happens!

⚙️ Function Playground

Choose a function below and give it an input!

Output will appear here… 🖥️

🎁 Inputs and Outputs

Functions Can Take Inputs and Give Outputs!

Just like a vending machine — put something in, get something out!

📥

Parameters — Giving Info IN

You can pass information into a function — like putting ingredients into a recipe. The function uses them to do its job!

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

greet("Sara")
# Hello, Sara!
📤

Return — Getting Answers OUT

A function can give you a result back! Like a vending machine — put money in, get a snack out! That result is called a return value.

💰Put in:
2 + 3
⚙️add(2,3)
calculates
🎉Get back:
5!
def add(a, b):
    return a + b

add(2, 3)  # → 5

💪 Why Functions are Awesome

4 Reasons Every Coder Loves Functions!

Once you start using them, you’ll never go back!

♻️

Reuse Code

Write once, use anywhere. No copying the same thing twice!

📦

Stay Organised

Group related steps together in one neat, named package!

🐛

Fix Bugs Fast

Fix in one place — it works everywhere the function is used!

🤝

Share with Friends

Teams can share functions — everyone builds faster together!


🧠 Quick Quiz!
🎯 Functions Quiz

4 questions · pick an answer to check!

⭐ Remember This!

📝

Functions are like recipes — write steps once, use anytime!

🏷️

Give your function a clear name that says what it does

📥

Functions can take inputs called parameters

📤

Functions can return results back to you!