Concepts / Default Argument Values

Default Argument Values

Default argument values make certain function parameters optional by specifying a value to use if the caller does not provide one.

  • Programming

One Function, Several Greetings

Suppose a greeting function usually needs to use the word Hello, but sometimes should use Hi or Greetings. A caller should not have to provide a greeting word every time the usual choice is wanted. A default argument value lets the function use a specified value when the caller leaves that argument out, while still allowing the caller to supply a different value when needed.

A default argument value makes a function parameter optional: the caller may provide a value, but does not have to provide one.

Choosing the Value at Call Time

check parameteryesnoFunction callArgument providedCaller valueDefault value
Which value does the parameter hold when the caller supplies an argument, and which value does it hold when the caller leaves the argument out?

The decision is made when the function is called. If the caller supplies an argument for the parameter, Python uses that supplied argument. If the caller does not supply one, Python uses the value written in the function definition as the default.

What do you think happens?

A greeting parameter has the default value Hello. What value will the parameter hold when the caller supplies Hi?

  • Hello
  • Hi
  • Both values at the same time
Reveal answer

Answer: Hi

The default is used only when the caller does not provide an argument. When an argument is provided, Python uses the caller's value.

Writing the Parameter Syntax

parameter_nameparameter=assignment operatordefault_valuefallback value
Where does the default value appear in a function definition, and how is it associated with its parameter?

The general pattern for a default argument is parameter_name = default_value. This pattern appears inside the parentheses of the function definition.

The equals sign connects one parameter name to the value Python should use when that parameter is not supplied by the caller. In a greeting function, a generated definition could use greeting = "Hello". The parameter is greeting, and its default value is Hello.

Tracing Two Calls

A Greeting with an Optional Word

A function has a greeting parameter whose default value is Hello. Determine the value held by greeting in two calls: one call leaves the argument out, and another supplies Greetings.

Call without an argument: The caller does not provide a value for greeting, so Python uses the default value Hello.

Call with an argument: The caller provides Greetings for greeting, so Python uses Greetings instead of the default.

The parameter holds Hello in the first call and Greetings in the second call.

caller supplies GreetingsgreetingHellogreetingGreetings
How does the selected value change when the caller moves from omitting the argument to supplying a caller value?

The default does not permanently force the parameter to hold one value. It supplies a fallback for calls that omit the argument. A call that provides an argument selects the provided value instead.

Safe Default Values

Default values must be immutable. The source identifies strings, numbers, None, and tuples as examples of immutable values that can be used as defaults. Following this constraint helps avoid bugs caused by unintended modifications.

Default categorySource guidanceReason to remember it
StringsListed as immutableSuitable as a default value
NumbersListed as immutableSuitable as a default value
NoneListed as immutableSuitable as a default value
TuplesListed as immutableSuitable as a default value

Mistakes with Optional Parameters

  • Assuming the default is used even when the caller supplies an argument.

    Python uses the provided argument when one is given.

    Fix: Use the default only in your prediction when the caller leaves that parameter out.

  • Putting the default value in the call instead of associating it with the parameter in the definition.

    The source defines this pattern as syntax inside the function definition.

    Fix: Write the parameter name, equals sign, and default value in the function definition.

  • Choosing a default without checking whether it is immutable.

    The source warns that unintended modifications can cause bugs.

    Fix: Choose an immutable default such as a string, number, None, or tuple.

  • Making every parameter mandatory even when a standard choice exists.

    The function loses the convenience provided by an optional parameter.

    Fix: Give the parameter a default when a sensible fallback is available.

Apply the Selection Rule

EASY

A function has a parameter named greeting with the default value Hello. Predict the value of greeting in each situation: the caller provides no greeting; the caller provides Hi; the caller provides Greetings. Then explain why each result occurs.

Hints
  • First check whether the caller supplied an argument.
  • Use the default only when the argument is omitted.
  • A supplied argument replaces the fallback for that call.

To predict a parameter's value, ask one question first: did the caller provide an argument for it? If yes, use the caller's value. If no, use the default written in the definition.

Design Checklist

  1. A default argument value makes a function parameter optional.
  2. Write a default using the pattern parameter_name = default_value inside the function definition.
  3. Python uses the caller's argument when one is supplied and the default when the argument is omitted.
  4. Choose immutable defaults; the source lists strings, numbers, None, and tuples as examples.
  5. Default values reduce the need to make callers provide a commonly used value repeatedly.

Key Takeaways

  • Default argument values provide fallback values for omitted function arguments.
  • The syntax is parameter_name = default_value in the function definition.
  • A supplied argument takes priority over the default for that call.
  • Immutable values such as strings, numbers, None, and tuples are identified as suitable default categories.
  • Use defaults when a parameter has a sensible common choice that callers should not have to repeat.