Concepts / Function Parameters

Function Parameters

When you use positional arguments, you must remember the exact order in which the function expects them. If a function takes five parameters, you need to count them correctly every time you call it. Keyword arguments eliminate this burden. You name each parameter explicitly, so the order no longer matters. This makes your code more readable and less error-prone, especially when a function has many parameters or when some of them have default values.

  • Programming

Why Position Can Mislead

A function call can provide values by position or by parameter name. Positional arguments are matched according to where they appear: the first value goes to the first parameter, the second value goes to the second parameter, and so on. This works, but the caller must remember the function's exact parameter order. Keyword arguments remove that burden by naming the parameter that should receive each value.

Imagine a function with several settings, such as a size, a color, and a layout choice. A positional call requires you to remember which setting belongs in each position. A keyword call makes each assignment visible, so a reader can identify the purpose of every supplied value directly from the call.

The central difference is the matching rule: positional arguments are matched by location, while keyword arguments are matched by an exact parameter name.

Positional Binding

With positional arguments, the interpreter reads the argument list from left to right. The value in the first position is assigned to the first parameter in the function definition, the value in the second position is assigned to the second parameter, and the pattern continues. Changing the order of the supplied values therefore changes which parameter receives each value.

matched by positionmatched by positionmatched by positionFirst valueposition 1First parameterSecond valueposition 2Second parameterThird valueposition 3Third parameter
What value is assigned to each parameter when arguments are supplied only by position?

Reading a Positional Call

A function expects three parameters in a particular order. A call supplies three values positionally.

First match: The first supplied value is assigned to the first parameter.

Second match: The second supplied value is assigned to the second parameter.

Third match: The third supplied value is assigned to the third parameter.

Order matters: If the caller rearranges the supplied values, the parameters receive different values, even though the same values were used.

A positional call is interpreted by location, so the caller must preserve the function's expected parameter order.

Named Argument Binding

A keyword argument supplies a value by naming the parameter that should receive it. The interpreter looks up that name and assigns the value directly to the matching parameter, ignoring the argument's position in the call.

Because the parameter name identifies the destination, keyword arguments can be written in any order. Reordering them does not change the mapping. This makes calls easier to read, particularly when a function has many parameters or when the purpose of a value is not obvious from its position.

matches bymatches byPositional callArgument locationKeyword callParameter name
What changes when values are assigned by name instead of by position?

Reordering Named Values

A function has parameters named size and color. A caller supplies values for both parameters using their names.

Name the destination: The value assigned to size is matched directly to the size parameter.

Name the other destination: The value assigned to color is matched directly to the color parameter.

Change the written order: Writing the color assignment before the size assignment still sends each value to the same named parameter.

Keyword arguments can be reordered because their parameter names, rather than their positions, determine the mapping.

Mixed Argument Calls

A single function call may combine positional and keyword arguments. The required order is strict: every positional argument must appear before every keyword argument. The positional values are matched first by location, and the named values are then matched directly to their parameter names.

positionpositionexact nameFirst valuepositionalFirst parameterSecond valuepositionalSecond parameterNamed valuekeywordNamed parameter
How does Python match each argument when a call mixes positional arguments with keyword arguments?

Use positional arguments when the order is clear and stable, then use keyword arguments for later values whose parameter names make the call easier to understand. This preserves the required ordering while making selected assignments explicit.

Defaults and Selective Overrides

Keyword arguments are especially useful when some parameters have default values. A caller can provide a keyword value for only the parameter that needs a different setting and rely on the function's defaults for the other parameters. This makes it possible to customize one part of a call without restating every available setting.

overridden by keywordcontinues as defaultSelected parameterKeyword valueOther parameterDefault value
Which parameters receive explicit keyword values, and which continue using their default values?

Changing One Setting

A function has several parameters, and some have default values. The caller wants to change only one of those settings.

Identify the desired change: Choose the parameter whose default behavior should be different for this call.

Supply its name: Pass the new value as a keyword argument using the exact parameter name.

Leave other parameters out: Do not provide values for the remaining defaulted parameters when their defaults are acceptable.

The named parameter receives the explicit value, while the omitted parameters continue using their default values.

Mistakes with Names and Order

  • Putting a positional argument after a keyword argument

    All positional arguments must come before all keyword arguments.

    Fix: Move the positional value before the keyword assignments, or give that value a parameter name as well.

  • Misspelling a parameter name

    The interpreter requires an exact parameter-name match and does not guess which similar name was intended.

    Fix: Check the function's parameter names and use the exact spelling.

  • Assuming keyword arguments still depend on their written order

    Keyword arguments are matched by name, not by position.

    Fix: Read the name attached to each value; correctly named keyword arguments can be written in any order.

  • Repeating values unnecessarily when defaults are suitable

    This obscures the one intentional override and creates more values that must be remembered.

    Fix: Provide the specific keyword override and allow the other parameters to use their defaults.

A keyword argument is only useful when its name exactly matches a parameter in the function definition. A near match is not treated as an alternative spelling; Python raises a TypeError instead of guessing.

Practice the Mapping

MEDIUM

Consider a function with five parameters. Describe how its values are matched in each situation: first, when five values are supplied only by position; second, when the same values are supplied with parameter names in a different order; third, when two initial values are positional and the remaining selected values are keyword arguments; and fourth, when one defaulted parameter is overridden while the other defaulted parameters are omitted.

Hints
  • For positional values, track the argument positions from left to right.
  • For keyword values, track the exact parameter name instead of the written order.
  • In a mixed call, place every positional value before every keyword value.
  • An omitted defaulted parameter keeps its default value.

What do you think happens?

If two correctly named keyword arguments are exchanged in the written order of a function call, do they switch which parameters receive their values?

  • Yes, because every argument is matched by position
  • No, because keyword arguments are matched by parameter name
  • Only when the function has default values
Reveal answer

Answer: No, because keyword arguments are matched by parameter name.

The interpreter ignores the keyword arguments' positions and assigns each value directly to the parameter with the exact supplied name.

Key Takeaways

  1. Positional arguments are assigned from left to right according to parameter order.
  2. Keyword arguments name their destination parameters, so their order in the call does not matter.
  3. When positional and keyword arguments are combined, all positional arguments must come first.
  4. A keyword name must exactly match a parameter name; otherwise Python raises a TypeError.
  5. Keyword arguments can override selected parameters while other parameters continue using their default values.

Key Takeaways

  • Positional calls depend on the exact order of parameters.
  • Keyword calls assign values by exact parameter name and can be written in any order.
  • Mixed calls are valid only when positional arguments appear before keyword arguments.
  • Keyword arguments make selective overrides and default values easier to use and understand.