Function Parameters: An Introduction to Positional, Keyword, and Default Parameters

In Python, a function is like a tool that can receive different “instructions,” and function parameters are the “information” we pass to this tool. Using different types of parameters reasonably can make functions more flexible and easier to use. Today, we’ll learn the three most basic and commonly used parameters: positional arguments, keyword arguments, and default arguments.

1. Positional Arguments

Positional arguments are the most basic parameter type and must be passed in the order defined by the function. Imagine telling a friend, “Give me 10 yuan and 5 yuan” – the order of “10 yuan” and “5 yuan” can’t be reversed, or the meaning changes.

When defining the function, parameters are listed in order:

def add(a, b):  # a and b are positional arguments
    return a + b

When calling the function, values must be passed in the same order:

result = add(3, 5)  # a=3, b=5, returns 8
print(result)  # Output: 8

Note: Positional arguments must be passed “exactly” – passing too few or too many will cause an error. For example, add(1) will prompt “missing argument b,” and add(1, 2, 3) will prompt “extra arguments.”

2. Keyword Arguments

Keyword arguments are passed in the form of parameter name = value and can be in any order, making the parameter meaning more explicit. For example, saying “Please give me the item priced at 5 yuan and the item priced at 10 yuan” clarifies with “price=5 yuan” and “price=10 yuan” to avoid order confusion.

When calling the function, use parameter name = value:

def greet(name, message="Hello"):  # name is a positional argument; message is a default argument (discussed later)
    print(f"{message}, {name}!")

# Call with keyword arguments (order can be reversed)
greet(name="Xiaoming", message="Hi")  # Output: Hi, Xiaoming!
greet(message="Hello", name="Xiaohong")  # Output: Hello, Xiaohong!

When mixing positional and keyword arguments, positional arguments must come first, followed by keyword arguments:

greet("Xiaogang", message="Good morning")  # Correct: positional argument first, keyword argument second
greet(message="Good morning", "Xiaogang")  # Error! Throws "SyntaxError"

3. Default Arguments

Default arguments provide a “backup value” for parameters. If the argument is not passed during the call, the default value is automatically used. For example, when calculating the area of a circle, the default radius is 3, so you don’t need to pass the radius every time.

Definition: Assign a default value with = after the parameter name. Default parameters must be placed at the end of the positional parameter list (otherwise, an error occurs):

def circle_area(radius=3):  # radius has a default value of 3 (must be at the end)
    return 3.14 * radius ** 2

# Call without radius (uses default value)
area1 = circle_area()  # 3.14 * 3² = 28.26
# Call with a new value to override the default
area2 = circle_area(5)  # 3.14 * 5² = 78.5

Note: The default value of a default parameter is determined when the function is defined, not regenerated each time the function is called. If the default parameter is a mutable object (e.g., a list or dictionary), it may cause unexpected behavior (e.g., repeated values), but beginners can ignore this for now (we’ll cover it in advanced topics later).

Rules for Mixing All Three Parameter Types

  1. Positional arguments first, then keyword arguments: For example, func(1, b=2) is valid, but func(b=2, 1) is invalid.
  2. Default parameters must be at the end: Avoid defining def func(a=1, b) (error), as default parameters cannot precede non-default parameters.

Scenario Summary

  • Positional arguments: For “must-pass critical information” (e.g., core logic parameters).
  • Keyword arguments: For scenarios with many parameters where clarity is key (e.g., avoiding order confusion when calling functions with long parameter lists).
  • Default arguments: For optional parameters where the same value is used most of the time, with occasional modifications (e.g., setting default units or values for a function).

By combining these three parameter types, you can write more concise and flexible functions. Practice calling functions with different parameters to master them quickly!

Xiaoye