Functions
📚 What are Functions? A function is a named, reusable block of code. You write it once with def function_name():, then call it as many times as you need just by writing its name. Functions can accept inputs (parameters) and send back a result with return. They are the most important tool for keepi…

Appy Says…
The moment you write the same code twice, you should make it a function. Functions are the building blocks of every real app — they let you organise code, reuse it, and change it in one place instead of ten.
What is a Function?
A function is a named, reusable block of code. You define it once with def, then call it by name whenever you need it. You can pass in data (parameters) and get a result back (return value).
- •
def function_name(parameters):— defines the function - •Indented code below is the body — what the function does
- •
return value— sends a result back to whoever called the function - •Call it with
function_name(arguments)— like pressing a button
Think of it like a power-up in a game
Imagine a heal_player() power-up. Whenever the player picks it up, the same code runs: add health, play a sound, show an animation. You write that once and just call heal_player() every time — you don't copy the whole block of code at every pick-up point in the level.
Anatomy of a Function
- •Parameters are the inputs listed inside the brackets when defining:
def greet(name): - •Arguments are the actual values you pass when calling:
greet("Alex") - •Return sends the result back — the function call becomes that value
- •A function with no
returngives backNone - •You can have default parameters:
def greet(name, msg="Hello"):
Real-World Examples
- •Spotify has a
play_track(track_id)function called every time you tap a song - •Instagram has
like_post(post_id, user_id)— it runs every time someone taps ❤️ - •Every game has a
calculate_score(hits, time_remaining)type function - •A web app has
send_email(to, subject, body)— reused across sign-up, reset, confirm…
Key Facts
- •Functions can call other functions — this is how large programs are organised
- •A function should do one thing well — short, focused functions are easier to debug
- •Python has hundreds of built-in functions you already use:
print(),len(),range() - •You can return multiple values:
return x, y— the caller gets a tuple - •A function that calls itself is called recursive — useful but needs a base case to stop
Watch Out: Define Before You Call
Python reads your code top to bottom. If you call a function before you define it, you'll get a NameError. Always put your def statements above the code that calls them.
Remember
If you find yourself writing the same code in more than one place, turn it into a function. This is called the DRY principle: Don't Repeat Yourself. Professional developers live by this.
What You Learned
- •
def name(params):defines a reusable block of code - •Call it with
name(arguments)— passing in the values it needs - •
return valuesends the result back to the caller - •Parameters are inputs; the return value is the output
- •DRY: Don't Repeat Yourself — if you write code twice, make a function
Key Facts
- →Functions can call other functions — this is how large programs are organised
- →A function should do one thing well — short, focused functions are easier to debug
- →Python has hundreds of built-in functions you already use:
print(),len(),range() - →You can return multiple values:
return x, y— the caller gets a tuple - →A function that calls itself is called recursive — useful but needs a base case to stop
Real-World Examples
Remember
If you find yourself writing the same code in more than one place, turn it into a function. This is called the DRY principle: Don't Repeat Yourself. Professional developers live by this.
Quick Quiz
How do you define a function in Python?