Python · Functions
Parameters and Arguments
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Parameters vs Arguments
So far, our functions have been simple - they always do exactly the same thing every time we call them. But what if we want a function that can work with different values? Parameters are how functions ask for information when they're called. Think of them as questions the function needs answered before it can do its job. For example, a
greet_codemon()
function needs to know which Codémon to greet! Here is an example:
def greet_codemon(codemon_name): print(f"Hello, {codemon_name}!") greet_codemon("Aquachu") greet_codemon("Pyrochu")
As you can see, the function
greet_codemon()
has a parameter called
codemon_name
. When we call the function, we provide different values for that parameter: first "Aquachu", then "Pyrochu". The function uses those arguments to customize its behavior.
Exercise 2 — Parameters vs Arguments
Actually, I lied to you a little bit. It's not just parameters, it's parameters and arguments!
- Parameters are the variables listed in the function definition - Arguments are the actual values you pass when calling the function So yeah, it's basically the same thing, but with different names depending on whether you're defining the function or calling it.
# 'codemon_name' is a PARAMETER def greet_codemon(codemon_name): print(f"Hello, {codemon_name}!") # 'Aquachu' is an ARGUMENT greet_codemon("Aquachu") # 'Pyrochu' is also an ARGUMENT greet_codemon("Pyrochu")
Think of parameters as empty variables waiting to get a value assigned. Arguments are the actual values you assign to those parameters when you call the function.
Exercise 3 — Identify Parameters and Arguments
Look at this code:
def train_codemon(codemon, hours): print(f"{codemon} trained for {hours} hours") train_codemon("Florachu", 3)
What are the parameters in this code?
Options
• ```codemon``` and ```hours```
• ```"Florachu"``` and ```3```
• ```train_codemon```
• ```codemon```, ```hours```, ```"Florachu"```, and ```3```
Hint : Remember: parameters are the variables in the function definition (the ```def``` line), not the values passed when calling the function.
Exercise 4 — Create a Function with a Parameter
Complete the function definition and function call to calculate a Codémon's power after leveling up. The function should take the current level as a parameter and print the new power (level * 10).
def calculate_power(____): power = ____ * 10 print(f"Power: {power}") # Call the function with level 5 calculate_power(____)
Options: level level 5 power
Hint : Think about what variable name makes sense for the parameter, and how to use that parameter inside the function. Then pass the correct value when calling.
Exercise 5 — Functions with Multiple Parameters
Functions can have multiple parameters separated by commas. When you call the function, you need to provide arguments in the same order as the parameters are defined.
def battle(attacker, defender, damage): print(f"{attacker} attacks {defender}!") print(f"{defender} takes {damage} damage!") # The order of arguments must match the order of parameters battle("Aquachu", "Pyrochu", 25) print("---") battle("Florachu", "Aquachu", 30)
The order is crucial! If you pass
battle(25, "Aquachu", "Pyrochu")
, Python will assign: -
attacker = 25
(wrong!) -
defender = "Aquachu"
(wrong!) -
damage = "Pyrochu"
(wrong!)
Exercise 6 — Create a Function with Multiple Parameters
Complete the function that displays information about a Codémon. It should have three parameters: name, type, and level.
def display_codemon(____, ____, ____): print(f"Name: {name}") print(f"Type: {type}") print(f"Level: {level}") # Call with appropriate arguments display_codemon(____, ____, ____)
Options: name type level "Aquachu" "water" 15
Hint : Define the three parameters in the order that makes sense (name, type, level), then pass arguments in that same order when calling the function.
Exercise 7 — Understanding Argument Order
Given this function definition:
def evolve_codemon(name, level, new_name): print(f"{name} at level {level} evolves into {new_name}!")
Which function call is correct?
Options
• ```evolve_codemon("Aquachu", 20, "Aquaking")```
• ```evolve_codemon(20, "Aquachu", "Aquaking")```
• ```evolve_codemon("Aquaking", "Aquachu", 20)```
• ```evolve_codemon("Aquachu", "Aquaking", 20)```
Hint : Look at the parameter order in the function definition: name first, then level, then new_name. Your arguments must follow the same order.
Exercise 8 — Match Function Concepts
Match each term with its correct description:
Items to match
• Parameter
• Argument
• Positional argument
• Function call
Definitions
• Variable in function definition
• Actual value passed when calling
• Argument matched by position/order
• Using the function with arguments
Exercise 9 — Default Parameter Values
Sometimes you want a parameter to have a default value that's used if no argument is provided. You can do this by assigning a value to the parameter in the function definition.
def create_codemon(name, type, level=1): print(f"Created {name}!") print(f"Type: {type}, Level: {level}") create_codemon("Aquachu", "water") print("---") create_codemon("Pyrochu", "fire", 1)
If you look at the example above and run it, you'll see that we call the function twice. The first time, we only provide the name and type, so the level defaults to 1. The second time, we provide all three arguments, so level is set to 1 explicitly. Technically we didn't need to provide level in the second call since it's already 1 by default, but I did it to show you it was the same.
Key points about default parameters: - Parameters with defaults are optional - you can omit them when calling - Parameters without defaults are required - you must provide them - Default parameters must come after required parameters
Common Use Cases
Default parameters are great for: - Configuration options - Optional features - Common default values For example, most Codémon start at level 1, so making it a default makes sense!
Exercise 10 — Create a Function with Default Parameters
Complete the function that heals a Codémon. The
potion_type
parameter should default to
"basic"
. If the potion is
"super"
, heal 50 HP, otherwise heal 30 HP.
def heal_codemon(name, potion_type____): if potion_type ____ "super": hp = ____ else: hp = 30 print(f"Healing {name} with {potion_type} potion: {hp} HP") # Call with default potion_type (basic) heal_codemon(____) # Call with super potion heal_codemon("Pyrochu", "super")
Options: ="basic" =="basic" == 50 30 "Aquachu" Aquachu ="super"
Hint : Remember: use ```=``` for default values in function definition, and ```==``` to compare values in conditions.
Exercise 11 — When Are Default Values Used?
Look at this function:
def catch_codemon(name, ball_type="codeball", difficulty=5): print(f"Trying to catch {name}") print(f"Using {ball_type}, difficulty: {difficulty}") catch_codemon("Florachu", "ultraball")
What will be the value of
difficulty
in this function call?
Options
• ```5``` (the default value)
• ```"ultraball"```
• ```None```
• An error will occur
Hint : When you don't provide an argument for a parameter with a default value, Python uses that default. Here, only 2 arguments are provided, so the third parameter uses its default value of 5.
Exercise 12 — Best Practices for Parameters
Here are some important guidelines to follow when working with parameters:
1. Use descriptive parameter names - Good:
def calculate_damage(attacker_power, defender_defense)
- Bad:
def calculate_damage(x, y)
2. Order parameters logically - Required parameters first - Optional parameters (with defaults) after - Most important parameters at the beginning
3. Don't use too many parameters - If a function needs more than 3-4 parameters, consider if it's doing too much - Maybe split it into smaller functions
# Good example: clear, ordered parameters def train_codemon(name, training_type, duration=1, boost=False): result = f"{name} trained in {training_type}" if boost: result += " with XP boost" result += f" for {duration} hour(s)" print(result) train_codemon("Aquachu", "battle") train_codemon("Pyrochu", "defense", 2, True)
Remember
Good parameter design makes your functions: - Easier to understand - Easier to use correctly - Easier to maintain and modify later Take time to think about your parameters!
Exercise 13 — Codémon Stats Display
Create a function to display Codémon battle statistics! Your tasks: 1. Define a function
show_stats()
with these parameters: -
name
(required) -
type
(required) -
level
(optional, default = 1) -
hp
(optional, default = 100) 2. The function should print all the stats in a formatted way 3. Call the function 3 times: - With only required parameters (Aquachu, water) - With required + level (Pyrochu, fire, level 5) - With all parameters (Florachu, plant, level 10, hp 150)
Starter code
# 1. Define show_stats() with required and optional parameters # 2. Inside the function, print the stats # 3. Call with only required parameters # 4. Call with required + level # 5. Call with all parameters
Back to the course