DAY 8

Functions 🏭⚡🔧

Write code once, use it forever. Functions are reusable machines that do your work on command!

⏱ 15 mins
⚡ +50 XP
Functions 🏭⚡🔧

Day 8: Functions — Build Once, Use Forever!

What's a Function?

Think of a coffee machine. You press one button and boom — coffee appears! You don't rebuild the machine every time. You just press the button. Functions work exactly like that — write the code once, then just "press the button" whenever you need it!

How to Create a Function


def greet():
    print("Hello Rohith")

def = create a function. greet = the function's name (your button). The indented code = what happens when you press it.

How to Use It


def greet():
    print("Hello Rohith")

greet()
greet()
greet()

Output: Hello Rohith, Hello Rohith, Hello Rohith. You wrote the code once but ran it three times. That's the magic!

Real World Connection

Every time you tap the Like button on Instagram, one function runs. Every time you hit Send on WhatsApp, one function runs. Every button in every app is a function waiting to be called!

Common Mistakes

Mistake 1 — Missing the colon.


def greet()    # WRONG — missing :
def greet():   # CORRECT

Mistake 2 — Creating but never calling it.


def greet():
    print("Hello")
# WRONG — function exists but never runs!

def greet():
    print("Hello")
greet()        # CORRECT — now it runs!

Mini Challenge

Mini Challenge

Create a function called morning_alarm that prints "Wake up!", "Brush teeth!", and "Eat breakfast!". Then call it 3 times. You just built a morning routine app!

Quick Quiz

Q: What keyword creates a function? A: def!

Q: I wrote a function but nothing happens. Why? A: You never called it! Add greet() at the bottom.

Q: Why use functions instead of just writing code directly? A: Write once, reuse forever. No copy-pasting!

Key Takeaways

Key Takeaways

  • Functions are reusable blocks of code — build once, use many times.
  • def creates the function. functionname() calls it.
  • Always add : after the function name and indent the code inside.
  • Creating a function doesn't run it — you must call it!
  • Every button in every app is a function!

← Previous Lesson