What Are Functions?
Letβs kick the door open.
Alright coder β up until now, your code has been doing everything line by line, like a very enthusiastic but slightly overwhelmed intern.
But now?
Now we give your code superpowers.
Functions are how you teach Python to:
- remember a task
- package it nicely
- repeat it ANYTIME you want
Itβs like training your code to do backflips on command.
1. The Big Idea
A function is basically you saying:
βHey Python, do this thing for me. And do it EXACTLY the same way every time.β
Itβs your own custom command.
2. The Basic Function Shape
Hereβs the simplest function ever:
def greet():
print("Hello, hero!")
greet()
def = βIβm defining a function.β
greet() = βDo the thing!β
Call it like this:
greet()
Boom.
Your code just followed your instructions like a loyal sidekick.
3. Why Functions Matter for Games
Functions let you create:
- attack systems
- healing systems
- movement systems
- shop menus
- inventory actions
- dialogue scenes
- enemy AI behaviors
Basically, everything cool.
Without functions, your game becomes a giant wall of code that screams for help.
With functions?
Your game becomes organized, powerful, and easy to expand.
4. Real Example: Attack Function
def attack():
print("You swing your sword!")
print("The enemy takes damage!")
##Call it anytime - the lines below run the function (do NOT type this line):
## here so Python ignores this line.
attack()
attack()
Your hero is now spamming attacks like a speed runner.
Practice (Your Turn!)
Mini-Challenge
Create a function called welcome() that prints:
βWelcome to the adventure!β
Your game now greets players like a friendly wizard.
You now understand:
- what functions are
- why they matter
- how they make your code cleaner
- how they power game systems
- how to create and call your first function
Your code officially has skills.
Ready for Step 2, where we add parameters β the feature that lets your functions take input and become 10x more powerful?

