What Even Is a Function?
Alright young coder imagine if every time you wanted your character to attack, you had to rewrite the entire attack code from scratch.
Every. Single. Time.
Your keyboard would file a complaint.
Your teacher would start questioning their life choices.
Functions fix that.
1. What Is a Function?
A function is a reusable block of code that does ONE job.
Think of it like:
- a spell in a spellbook
- a special move in a video game
- a button you press that makes magic happen
You write it once.
You use it whenever you want.
Zero copy-paste. Zero drama.
2. Basic Structure
def greet():
print("Hello, hero!")
def = βIβm defining a new superpower.β
greet() = βUse the superpower.β
3. Calling a Function (a.k.a. Pressing the Magic Button)
greet()
Boom.
Instant greeting.
Your code is now efficient AND polite.
4. Why Functions Matter in Games
Functions let you create:
- attack moves
- healing abilities
- shop systems
- enemy behavior
- level transitions
- dialogue scenes
- inventory actions
Basically, everything cool in a game is powered by functions.
Practice (Your Turn!)
Mini Challenge
Create a function called train() that prints:
“You train hard and gain XP!”
Then call it twice.
Your code just did the same action twice with ONE definition.
Thatβs the power of functions.

