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:
With Return ๐: The function calculates and gives you a result
def calculate_damage(attack, defense): return attack - defense # Returns a valueWithout 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 anythingWhen 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!