Intro to Functions

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — What are Functions?

Imagine you're training multiple Codémon and need to display a welcome message for each one. Without functions, you'd have to write the same code over and over:

print("============================") print("Welcome to Codémon Training!") print("============================") print("Training Aquachu...") print("============================") print("Welcome to Codémon Training!") print("============================") print("Training Pyrochu...") print("============================") print("Welcome to Codémon Training!") print("============================") print("Training Florachu...")

This is repetitive and hard to maintain. What if you want to change the message? You'd have to update it in multiple places! Functions solve this problem by letting you write code once and reuse it many times. This follows the DRY principle: Don't Repeat Yourself.

Why Functions Matter

Functions help you: - Write less code - Make your code easier to read - Fix bugs in one place instead of many - Organize your code better

Exercise 2 — Creating Your First Function

In Python, we create functions using the def keyword (short for "define"). Here's the basic syntax:

def function_name(): # Code to execute goes here # This code is indented

Let's break this down: - def tells Python we're creating a function - function_name is what we call our function (you choose this!) - () are parentheses (required, even if empty for now) - : marks the end of the function header - The code inside must be indented (usually 2 or 4 spaces)

Here's a real example:

def show_welcome(): print("============================") print("Welcome to Codémon Training!") print("============================") # To use the function, we "call" it: show_welcome()

Notice how we: 1. Define the function with def show_welcome(): 2. Call the function with show_welcome() The function doesn't run until we call it!

Common Mistakes

- Forgetting the colon : after the function name - Not indenting the code inside the function - Forgetting the parentheses () when calling the function - Last but not least, forgetting to call the function!

Exercise 3 — Identifying Valid Function Syntax

Which of the following is the correct way to define a function in Python?

Options
• def greet_codemon(): print("Hello, Codémon!")
• def greet_codemon() print("Hello, Codémon!")
• def greet_codemon: print("Hello, Codémon!")
• function greet_codemon(): print("Hello, Codémon!")

Hint : Remember: functions need the def keyword, parentheses (), a colon :, and indented code inside.

Exercise 4 — Function Parts and Their Names

Match each part of a function with its correct name:

Items to match
• def
• show_message
• ()
• :
Definitions
• Keyword to create a function
• Function name
• Parentheses (required after function name)
• Colon (marks end of function header)

Exercise 5 — Complete the Function Definition

Complete the code to define a function that displays a training message:

Hint : Remember the basic syntax: def function_name(): followed by indented code.

Exercise 6 — Understanding Function Calls

What happens when you define a function in Python?

def say_hello(): print("Hello!")
Options
• Nothing is printed - the function is just stored in memory
• "Hello!" is printed immediately
• An error occurs
• The function runs automatically every time the program starts

Hint : Defining a function doesn't run it. You need to call it with function_name() to execute the code inside.

Exercise 7 — Create a Complete Function

Complete this code to create and call a function that displays a Codémon's power level:

Hint : First define the function with def, then call it using the function name with parentheses.

Exercise 8 — Calling Functions Multiple Times

The real power of functions is that you can call them as many times as you want! Let's see this in action:

def battle_cry(): print("⚡ Codémon, attack! ⚡") # Call it three times battle_cry() battle_cry() battle_cry()

See how we wrote the code once but used it three times? This is much better than copying and pasting the same code (especially with real code and big functions)! You can call a function anywhere in your code after it's been defined, and as many times as you need.

Function Naming Convention

Function names should: - Use lowercase letters - Use underscores between words (snake_case) - Be descriptive of what the function does - Examples: calculate_damage(), heal_codemon(), show_stats()

Exercise 9 — Identify the Correct Code

Which code correctly defines and calls a function that displays "Codémon healed!"?

Options
• def heal(): print("Codémon healed!") heal()
• def heal() print("Codémon healed!") heal()
• def heal(): print("Codémon healed!") heal()
• def heal(): print("Codémon healed!") heal

Hint : Check for: colon after function definition, proper indentation, and parentheses when calling.

Exercise 10 — Fix the Broken Function

This code has several errors. Fix them to make the function work correctly:

Hint : Remember: def keyword, colon, proper indentation for both lines, and parentheses when calling.

Exercise 11 — Codémon Battle Simulator

Create a battle simulator using a function and a loop! Your tasks: 1. Print "=== Battle Started ===" 2. Define a function fight() that prints "⚔️ Aquachu attacks!" 3. Use a loop to call fight() 5 times (simulate 5 attacks) 4. Print "=== Victory! ==="

Starter code
# 1. Print battle start message # 2. Define fight() function # 3. Loop 5 times and call fight() each time # 4. Print victory message
Back to the course