Module 17: Advanced Parameters

Explore default values and keyword arguments for flexible functions.

Anatomy: Default Parameters Abstraction

Functions can have optional parameters with default values. Click each part of this definition to see how it works.

def power_up(character, level=1):
    # Increase power level

Identify all 9 parts!

Spot the Keyword Argument

You can pass arguments by position or by name (keyword). Click on the argument(s) below that are passed using the parameter_name=value syntax (keyword arguments).

# Function definition
def create_profile(user_id, active=True): ...
create_profile(101) # Call 1 (Positional)
create_profile(102, False) # Call 2 (Positional)
create_profile(user_id=103) # Call 3 (Keyword)
create_profile(user_id=104, active=False) # Call 4 (Keyword)

Simulator: Defaults & Keywords Abstraction

Below is a function `configure_system` with default parameters. Your task: Call this function to set the `retries` parameter to `5`, but let `timeout` use its default value.

def configure_system(mode="auto", timeout=30, retries=3):
    print(f"Mode: {mode}, Timeout: {timeout}, Retries: {retries}")
Output will appear here...

Advanced Mini Missions! Decomposition

Combine your knowledge of defaults and keywords to solve these challenges.

Mission 1: Greeting with Default

Define a function `greet_user(name, greeting="Hi")`. Then, call it once just with the name `"Alex"` (using the default greeting).

Mission 2: Keyword Power

A function `set_config(ip_address, port=80, protocol="http")` exists. Call this function providing `"192.168.1.100"` for `ip_address` and `"https"` for `protocol` using keyword arguments (let `port` use its default).

Mission 3: Flexible Plotting

Define `plot_data(data, color="blue", linestyle="-")`. Call it twice: first with just `my_data` (imagine this variable exists). Second time, call it with `my_data`, setting `color` to `"red"` using a keyword argument.

Module 17 Advanced Practice Complete!

You've mastered defaults and keywords, making your function calls much more flexible! Great job stepping up the complexity. Abstraction