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!
“chocolate”
“Cake ready!”
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. 😊
print("Hello, Ali!")
print("Welcome!")
print("Have fun!")
print("Hello, Sara!")
print("Welcome!")
print("Have fun!")Repeating yourself — messy!def greet(name):
print("Hello,", name)
print("Welcome!")
print("Have fun!")
greet("Ali")
greet("Sara")Clean, short, reusable! 🎉2 Simple Steps!
Define it once — call it as many times as you want!
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!")
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!
Interactive Function Machine!
Pick a function, type an input, press Run — see what happens!
Choose a function below and give it an input!
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.
2 + 3
calculates
5!
def add(a, b): return a + b add(2, 3) # → 5
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!
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!
