Quest 14: Functions Part 2 - Side Effects & Procedures

Functions That Do Things Without Returning Values

โœจ QUEST 14 | Difficulty: Intermediate | Time: 5 minutes

๐Ÿ“Š Complexity Level: Intermediate โญโญ

Builds on fundamental concepts from earlier quests. Best for students who have completed Quests 1-6 or have some basic programming experience. This explores different types of functions.

๐Ÿ’ป Interactive Options:

  • ๐Ÿ““ Open in JupyterLite - Full Jupyter environment in your browser
  • โ–ถ๏ธ Run code directly below - All code cells on this page are editable and runnable
  • ๐Ÿ“ฅ Download Notebook - For use in local Jupyter or Google Colab

๐Ÿ“– Introduction: Actions vs Rewards

In Quest 6, we learned functions that return values (like spells that give you rewards). But not all functions need to return something. Some just need to do something.

Two types of function outcomes:

  1. With Return ๐ŸŽ: The function calculates and gives you a result

    def calculate_damage(attack, defense):
        return attack - defense  # Returns a value
  2. Without Return ๐ŸŽญ: The function performs an action (side effect)

    def announce_victory(winner_name):
        print(f"{winner_name} wins!")  # Doesn't return anything

๐ŸŽช Story Time: Imagine a tavern where the Barkeep has two jobs. One job is mixing a potion (returns the potion). Another job is announcing the next quest to the crowd (just tells everyone, doesnโ€™t hand you anything). Both are important functions!

๐Ÿ’ก Explanation: Side Effects

A side effect is something a function does besides returning a value: - Print to screen - Modify a list or variable - Write to a file - Display an image - Update game state

๐ŸŽฏ Functions Without Return Values

These functions often have print() statements or modify variables outside the function.

def level_up_player(player_stats):
    """Modifies player_stats in place"""
    player_stats['level'] += 1
    player_stats['experience'] = 0
    print(f"๐ŸŒŸ Level up! Now level {player_stats['level']}")
    # Notice: No 'return' statement!

# The function CHANGED the data but didn't return anything

When return is omitted, the function returns None:

result = level_up_player(player)
print(result)  # Prints: None

๐ŸŽฎ Activity 1: Display Functions

Functions that show information to the player:

๐ŸŽฎ Activity 2: Modifier Functions

Functions that change data:

๐ŸŽฎ Activity 3: Mixed Functions

Often youโ€™ll want both: side effects AND return values:

๐Ÿงฉ Challenge: Build a Logging Function

Write a function that: 1. Takes 3 parameters: message, log_level (like โ€œINFOโ€, โ€œWARNINGโ€, โ€œERRORโ€), and player_name 2. Prints a formatted log message with timestamp-like format 3. Does not return anything 4. Shows different emoji based on log level: - INFO: ๐Ÿ“ - WARNING: โš ๏ธ - ERROR: ๐Ÿ”ด

โœ… Solution

๐ŸŽ“ Key Concepts

โœจ Functions can do things without returning values (side effects) โœจ Common side effects: printing, modifying lists, updating state โœจ Some functions do BOTH: side effects AND return values โœจ A function without return returns None โœจ Side effect functions are great for โ€œactionsโ€ in games!

๐Ÿš€ Whatโ€™s Next?

Youโ€™ve learned functions that donโ€™t return, and earlier you learned functions that do. Next up: Quest 15: Functions Part 3 - Returning Functions where we dive deeper into writing excellent return-value functions!