What Even Is a Function?

STEP 1 β€” What Even Is a Function? (a.k.a. Your Code’s Superpower Factory)

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.

Modify and Run

Your code just did the same action twice with ONE definition.

That’s the power of functions.