Concepts / Function Parameters

Function Parameters

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

  • 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.

In the real world

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.

Key point

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.

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

Reading a Positional Call

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

  1. 1
    First match

    The first supplied value is assigned to the first parameter.

  2. 2
    Second match

    The second supplied value is assigned to the second parameter.

  3. 3
    Third match

    The third supplied value is assigned to the third parameter.

  4. 4
    Order matters

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

Result

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

Named Argument Binding

Definition

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.

Positional callArgument locationKeyword callParameter namematches bymatches by
What changes when values are assigned by name instead of by position?
Worked example

Reordering Named Values

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

  1. 1
    Name the destination

    The value assigned to size is matched directly to the size parameter.

  2. 2
    Name the other destination

    The value assigned to color is matched directly to the color parameter.

  3. 3
    Change the written order

    Writing the color assignment before the size assignment still sends each value to the same named parameter.

Result

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.

First valuepositionalFirst parameterSecond valuepositionalSecond parameterNamed valuekeywordNamed parameterpositionpositionexact name
How does Python match each argument when a call mixes positional arguments with keyword arguments?
Best practice

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.

Selected parameterKeyword valueOther parameterDefault valueoverridden by keywordcontinues as default
Which parameters receive explicit keyword values, and which continue using their default values?
Worked example

Changing One Setting

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

  1. 1
    Identify the desired change

    Choose the parameter whose default behavior should be different for this call.

  2. 2
    Supply its name

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

  3. 3
    Leave other parameters out

    Do not provide values for the remaining defaulted parameters when their defaults are acceptable.

Result

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

Mistakes with Names and Order

  • Common mistake

    Putting a positional argument after a keyword argument

    What it looks like

    A call begins with a named assignment and then supplies an unnamed value.

    Why it fails

    All positional arguments must come before all keyword arguments.

    Do this instead

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

  • Common mistake

    Misspelling a parameter name

    What it looks like

    A keyword uses a name that resembles a parameter but does not exactly match it.

    Why it fails

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

    Do this instead

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

  • Common mistake

    Assuming keyword arguments still depend on their written order

    What it looks like

    A learner thinks reversing two correctly named keyword assignments changes their destinations.

    Why it fails

    Keyword arguments are matched by name, not by position.

    Do this instead

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

  • Common mistake

    Repeating values unnecessarily when defaults are suitable

    What it looks like

    A caller supplies every parameter even though only one setting needs to change.

    Why it fails

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

    Do this instead

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

Edge case

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

At a glance

  • 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.

Related concepts

More concepts starting with F