What Are Functions?

UNIT 9, STEP 1 IS HERE, and this is where your code stops being a chaotic spaghetti monster and starts behaving like a well-trained ninja squad.

Let’s kick the door open.

STEP 1 β€” What Are Functions? (a.k.a. Teaching Your Code to Do Tricks)

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.

Click Run

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.

Click Run

Practice (Your Turn!)

Mini-Challenge

Create a function called welcome() that prints:

β€œWelcome to the adventure!”

Modify and Run

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?