Python online course

Default Parameter Values in Python Functions

Learn how Python function default parameter values work with positional and keyword arguments, required parameters, ordering rules, examples, and troubleshooting.

A function parameter is a named variable in a function definition that receives a value when the function is called. An argument is the value supplied in that call.

A default parameter value is a value assigned to a parameter in the function definition. Python uses that value when the caller does not provide an argument. This makes the parameter optional.

Default values are useful when a function has a standard choice that works for most calls, while still allowing callers to choose a different value when needed.

Defining a Default Parameter

Place an equals sign and the fallback value after the parameter name in the function signature:

def greet(name="friend"):
    print("Hello,", name)

The function signature is the function name and its parameter list in the definition. Here, name="friend" defines a parameter named name with the default value "friend".

The default is assigned in the function definition, not at the call site. A caller can omit the argument or provide a replacement value.

Calling a Function Without Optional Arguments

When every parameter has a default, the function can be called with no arguments:

def describe_me(name="Tuna", gender="M", website="geek-university.com"):
    print(name, gender, website)

describe_me()

Output:

Tuna M geek-university.com

Because the call supplies no arguments, Python gives name, gender, and website their defined default values.

Overriding Default Values

An explicitly supplied argument takes precedence over the default. This call supplies a value for every parameter:

def describe_me(name="Tuna", gender="M", website="geek-university.com"):
    print(name, gender, website)

describe_me("Tanya", "F", "tanya.com")

Output:

Tanya F tanya.com

All three defaults are replaced because the caller supplies all three arguments.

Required and Optional Parameters Together

A required parameter has no default value and must receive an argument. An optional parameter has a default value and may be omitted.

For example, a profile may require a name and gender but use a standard website unless the caller provides another one:

def describe_me(name, gender, website="geek-university.com"):
    print("Name:", name)
    print("Gender:", gender)
    print("Website:", website)

describe_me(name="Tanya", gender="F")

Output:

Name: Tanya
Gender: F
Website: geek-university.com

name and gender receive values from the call. The omitted website parameter receives its default.

Parameter Ordering Rule

Parameters without defaults must come before parameters with defaults. A required parameter cannot follow a defaulted parameter in a function signature.

def invalid_example(website="geek-university.com", name):
    pass

This definition raises a syntax error because name is required but appears after the defaulted website parameter.

The valid arrangement is:

def valid_example(name, website="geek-university.com"):
    pass

This ordering lets Python determine which positional arguments are required and which trailing positional arguments may be omitted.

How Python Chooses a Parameter Value

Argument omitted for a defaulted parameter: The parameter receives its default value because the caller supplied nothing for it.

Positional argument supplied: The value is matched to a parameter according to its position in the call.

Keyword argument supplied: The value is matched to the parameter whose name appears in the call.

Required argument omitted: Python cannot call the function and reports a missing required argument error.

Positional Arguments

A positional argument is matched to a parameter according to its position. The first argument goes to the first parameter, the second goes to the second parameter, and so on.

def describe_me(name, gender, website="geek-university.com"):
    print("Name:", name)
    print("Gender:", gender)
    print("Website:", website)

describe_me("Tanya", "F")

Python assigns "Tanya" to name, "F" to gender, and uses the default for website.

Positional calls must follow the parameter order. Reversing values is syntactically valid, but can produce incorrect meaning:

describe_me("F", "Tanya")

Python still assigns by position, so name becomes "F" and gender becomes "Tanya". The website still receives its default. Python cannot infer that the values were intended to be reversed.

Keyword Arguments

A keyword argument supplies a value using the parameter name, such as name="Tanya". Names, rather than positions, determine the assignments.

describe_me(name="Tanya", gender="F")

This call leaves website at its default. Keyword arguments may also be written in a different order:

describe_me(gender="F", name="Tanya")

The result is the same because both values are labeled with their parameter names. This differs from positional arguments, whose order matters.

Positional and Keyword Arguments Compared

How values are matched: Positional arguments use location in the call; keyword arguments use the parameter name.

Whether argument order matters: Positional argument order matters; keyword argument order does not matter when each argument is named.

Readability for similar values: Positional calls can be readable for a few clearly ordered parameters. Keyword calls are clearer when values have similar types or meanings.

Use when omitting an optional argument: Keyword arguments make it easy to provide a later optional parameter while identifying exactly which parameter receives the value.

Choosing Between Positional and Keyword Calls

  • Use positional arguments when there are only a few parameters and their order is obvious.
  • Use keyword arguments when values have similar types or meanings, because the parameter names document each value.
  • Prefer keyword arguments when skipping an optional parameter or when a value's meaning is not obvious from its position.
  • Use a consistent style within a project so function calls remain easy to read.

Common Problems and Fixes

Values appear in the wrong output fields

The likely cause is that positional arguments were passed in an order different from the parameter order. Compare the call with the function signature. Pass the values in the correct order or use keyword arguments to label them.

# Clearer when the values could be confused
describe_me(name="Tanya", gender="F")

A required value is missing

Find parameters in the signature that do not include an equals sign and a default value. Each such parameter needs an argument.

def describe_me(name, gender, website="geek-university.com"):
    pass

# name and gender are required
describe_me(name="Tanya", gender="F")

To fix a missing-argument error, provide the missing value either positionally or by keyword.

The definition has a default-argument syntax error

Inspect the parameter list for a required parameter after a defaulted parameter. Move all required parameters before parameters that have defaults.

A default was expected, but another value was printed

Check the call for both positional and keyword arguments. If the caller explicitly supplied a value for that parameter, Python uses the supplied value instead of the default. Omit the optional argument when the default should be used.

Exam-Relevant Notes

  • parameter_name=default_value is written in the function definition.
  • A default makes a parameter optional for callers.
  • An explicitly supplied argument overrides its default.
  • Required parameters must appear before defaulted parameters.
  • Positional arguments are order-sensitive.
  • Keyword arguments identify parameters by name, so their order does not matter.
  • Omitting a required argument causes a call-time error.