Level 5: Functions - IncrediCoders/Python1 GitHub Wiki

Intelli_Avatar_1

Intelli-Scents added this page on March 14, 2025

Here is a bonus article that will tell you more about Functions.

Learn About Functions

A function is a reusable piece of code that does something specific. You can give it a name and then use it anytime you need that action again, like when pressing a button.

Instead of writing the same steps over and over, you just call the function. It helps you keep your code organized, clean, and easy to read.

Functions vs Arguments

  • A function is a set of instructions with a name. It tells the computer: “Here’s what to do when this is called.”
  • An argument is the information you give to a function. It tells the function: “Here’s what to use when you do it.”

We have previously discussed what an Argument is at level 3. You can review it here: Level 3: Arguments

Why Functions Matter

Functions save time. Once you write a function, you can use it as many times as you want just by calling its name. This is super helpful when your program starts getting bigger.

Python Example

In Python, we create a function using the word def, followed by the function name and parentheses.

Scenario: Paul Python wants to greet his classmates every morning.

def say_hello():
    print("Good morning, class!")

# Call the function
say_hello()
say_hello()

This will print:

Good morning, class!
Good morning, class!
  • say_hello is the function.
  • Every time we call it, it prints the message.
  • No need to retype the same thing again and again!

Comparing Programming Languages

Most programming languages have functions, though they might use different keywords or structure.

Python

def welcome():
    print("Welcome!")

Java

public void welcome() {
    System.out.println("Welcome!");
}

C#

public void Welcome() {
    Console.WriteLine("Welcome!");
}

What’s the Difference?

  • Python is the simplest; just use def and start writing.
  • Java and C# require more setup (public, void, etc.) but do the same thing.
  • In all languages, a function is like a named action you can run again and again.

Functions are like magical tools in your program. Once you build them, they’re ready to go whenever you need them.

Learn More

TBD

Next Steps

Next, you can take on the two extra challenges to chase the Creeper and learn more! When you're done, you can move on to Level 6, the Boss Battle!

Take the Challenges!

  1. Challenge 1: In this challenge, you are going to write code that will add a time limit for 45 seconds for each stage of the game.

  2. Challenge 2: In this challenge, you are going to add batteries to the game. Paul collects the batteries to regain his health.

  3. Bonus Challenge: In this extra challenge, you'll see how to make your own levels for Paul to run and jump (and fly) through! Practice your game design skills!

More Level 5 Resources

  • Level 5: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game,

  • Level 5: Rewards - If you completed the Creeper Chase program that we talked about, then I set up this page to act as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the **WHAT **Award digital download, to show off your accomplishment!

  • Level 5: Creeper Chase: All Online Articles - Return back to the main Level 5 page, with all our online resources!

Level 6

After you're completely done with Level 5 (did you do the challenges?), then it's time to move on to Level 6! While you read through Level 6 in your book, you can check out the resources from Paul Python, as he teaches you how to build the Boss Battle program:

I hope you had fun learning about the Creeper Chase!

--