Function Parameters

Master different types of parameters and learn how to use them effectively in your functions.

45 minutes Beginner Functions

Required Parameters

Understanding Required Parameters

Definition

Required parameters are the basic building blocks of function arguments. They must be provided when calling the function, and their order matters.

Explanation

Think of required parameters like mandatory fields in a form. Just as you can't submit a form without filling in required fields, you can't call a function without providing all its required parameters.

def add(x, y):
    """Add two numbers together."""
    return x + y

# Function call with required parameters
result = add(5, 3)  # Returns 8
print(result)

# This would cause an error:
# add(5)  # TypeError: add() missing 1 required positional argument: 'y'

Default Parameters

Understanding Default Parameters

Definition

Default parameters allow you to specify values that will be used if no argument is provided for that parameter. They make functions more flexible and easier to use.

Explanation

Default parameters are like having a backup plan. If someone doesn't provide a specific value, the function will use the default value instead. This is particularly useful for optional settings or common use cases.

def greet(name, greeting="Hello"):
    """Greet someone with a custom or default message."""
    return f"{greeting}, {name}!"

# Using default parameter
print(greet("Alice"))  # Output: Hello, Alice!

# Overriding default parameter
print(greet("Bob", "Hi"))  # Output: Hi, Bob!

Variable Arguments (*args)

Understanding Variable Arguments

Definition

Variable arguments allow a function to accept any number of positional arguments.

Explanation

Variable arguments are useful when you don't know how many arguments will be passed to a function. They allow you to handle a varying number of inputs.

def sum_all(*args):
    """Sum all provided numbers."""
    return sum(args)

# Using variable arguments
print(sum_all(1, 2, 3))  # Output: 6
print(sum_all(1, 2, 3, 4, 5))  # Output: 15

def print_args(*args):
    """Print all arguments with their types."""
    for arg in args:
        print(f"Value: {arg}, Type: {type(arg)}")

print_args(1, "hello", 3.14, True)

Keyword Arguments (**kwargs)

Understanding Keyword Arguments

Definition

Keyword arguments allow a function to accept any number of named arguments.

Explanation

Keyword arguments are useful when you want to pass arguments to a function without knowing the exact order of the arguments. They allow you to specify the argument name.

def create_profile(**kwargs):
    """Create a user profile with any number of attributes."""
    return kwargs

# Using keyword arguments
profile = create_profile(
    name="John",
    age=30,
    city="New York",
    occupation="Developer"
)
print(profile)

def print_kwargs(**kwargs):
    """Print all keyword arguments."""
    for key, value in kwargs.items():
        print(f"{key}: {value}")

print_kwargs(first="Hello", second="World")

Parameter Order

Understanding Parameter Order

Definition

Understanding the correct order of parameters in Python functions.

Explanation

The order of parameters in a function is important. Python functions follow a specific order: required parameters, *args, default parameters, **kwargs.

def complex_function(required1, required2, *args, default1="value1", default2="value2", **kwargs):
    """Demonstrate parameter order in Python functions."""
    print(f"Required: {required1}, {required2}")
    print(f"Args: {args}")
    print(f"Defaults: {default1}, {default2}")
    print(f"Kwargs: {kwargs}")

# Valid call
complex_function(1, 2, 3, 4, default2="custom", extra="value")

# Invalid call (will raise error)
# complex_function(1, default1="custom", 2)  # SyntaxError

Type Hints

Using Type Hints

Definition

Using type hints to document expected parameter types.

Explanation

Type hints help with IDE support and documentation. They provide a way to specify the expected type of a parameter.

from typing import List, Union, Dict

def process_data(
    numbers: List[Union[int, float]],
    options: Dict[str, bool] = None
) -> float:
    """Process a list of numbers with optional settings."""
    if options is None:
        options = {}
    return sum(numbers) / len(numbers)

# Type hints help with IDE support and documentation
result = process_data([1, 2, 3.5], {"verbose": True})

Parameter Validation

Validating Parameters

Definition

Validating parameters before processing them in the function.

Explanation

Parameter validation is important to ensure that the function receives valid input. It helps prevent errors and unexpected behavior.

def calculate_percentage(value: float, total: float) -> float:
    """Calculate percentage with parameter validation."""
    if not isinstance(value, (int, float)) or not isinstance(total, (int, float)):
        raise TypeError("Both parameters must be numbers")
    if total == 0:
        raise ValueError("Total cannot be zero")
    if value < 0 or total < 0:
        raise ValueError("Values cannot be negative")
    
    return (value / total) * 100

# Valid calls
print(calculate_percentage(50, 100))  # 50.0
print(calculate_percentage(75, 150))  # 50.0

# Invalid calls
# calculate_percentage("50", 100)  # TypeError
# calculate_percentage(50, 0)      # ValueError

Mutable Default Parameters

Understanding Mutable Default Parameters

Definition

Understanding the pitfalls of using mutable objects as default parameters.

Explanation

Mutable default parameters can lead to unexpected behavior. It's better to use None as a default and create the object inside the function.

# Incorrect way (using mutable default)
def add_to_list(item, my_list=[]):
    my_list.append(item)
    return my_list

# Correct way (using None as default)
def add_to_list_safe(item, my_list=None):
    if my_list is None:
        my_list = []
    my_list.append(item)
    return my_list

# Demonstration
print(add_to_list(1))  # [1]
print(add_to_list(2))  # [1, 2]  # Unexpected!

print(add_to_list_safe(1))  # [1]
print(add_to_list_safe(2))  # [2]  # Expected behavior

Parameter Unpacking

Using Unpacking Operators

Definition

Using unpacking operators to pass arguments to functions.

Explanation

Unpacking operators allow you to pass multiple arguments at once. They can be used with tuples, lists, and dictionaries.

def print_coordinates(x, y, z):
    """Print 3D coordinates."""
    print(f"X: {x}, Y: {y}, Z: {z}")

# Using unpacking
point = (10, 20, 30)
print_coordinates(*point)  # Unpacking tuple

# Dictionary unpacking
def create_user(name, age, city):
    return {"name": name, "age": age, "city": city}

user_data = {"name": "John", "age": 30, "city": "New York"}
user = create_user(**user_data)  # Unpacking dictionary

Parameter Documentation

Best Practices for Documentation

Definition

Best practices for documenting function parameters.

Explanation

Good documentation helps others understand your code and makes it easier to maintain. It's important to include clear descriptions and type hints.

def calculate_discount(
    price: float,
    discount_percent: float,
    min_purchase: float = 0
) -> float:
    """
    Calculate the discounted price for a product.
    
    Args:
        price (float): Original price of the product
        discount_percent (float): Discount percentage (0-100)
        min_purchase (float, optional): Minimum purchase amount for discount.
            Defaults to 0.
    
    Returns:
        float: Final price after discount
    
    Raises:
        ValueError: If discount_percent is not between 0 and 100
        TypeError: If any parameter is not a number
    """
    if not 0 <= discount_percent <= 100:
        raise ValueError("Discount must be between 0 and 100")
    if price < min_purchase:
        return price
    return price * (1 - discount_percent / 100)
Concept 1 of 10