Python online course

Python Positional Arguments in Functions

Learn how Python matches positional arguments to function parameters, handles required values, and reports missing, extra, or incorrectly ordered arguments.

What Are Parameters and Arguments?

A function definition creates a reusable function and declares the names of the values it can receive. A function call runs that function and supplies values for it to use.

A parameter is a named variable in a function definition. An argument is a value passed to the function during a call.

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

greet("Tuna")

In this example, name is a parameter. The string "Tuna" is an argument. When the function runs, the argument value becomes available through the parameter name inside the function body.

A function can define multiple parameters:

def describe_person(name, gender):
    print("My name:", name)
    print("My gender:", gender)

The function signature is the function name together with its parameter list. In this example, the signature is represented by describe_person(name, gender).

How Positional Arguments Are Matched

A positional argument is matched to a parameter according to its location in the function call. Python assigns arguments from left to right:

  1. The first argument is assigned to the first parameter.
  2. The second argument is assigned to the second parameter.
  3. The third argument is assigned to the third parameter.
  4. This continues for each supplied argument.

The order of the parameters in the function definition establishes the required order for a positional call.

def describe_person(name, gender):
    print("My name:", name)
    print("My gender:", gender)

describe_person("Tuna", "M")

Before the function body runs, Python creates these bindings:

Function parameter positionParameter nameArgument position in callArgument value
1name1"Tuna"
2gender2"M"

As a result, name refers to "Tuna", and gender refers to "M" while this execution runs.

Defining and Calling a Function With Two Parameters

Here is the complete example and its output:

def describe_person(name, gender):
    print("My name:", name)
    print("My gender:", gender)

describe_person("Tuna", "M")
My name: Tuna
My gender: M

The first value, "Tuna", is assigned to name. The second value, "M", is assigned to gender. Only after these assignments can the two print() statements use the parameter values.

Reusing a Function With Different Inputs

A function definition can be called many times. Each call receives its own set of parameter bindings for that execution.

def describe_person(name, gender):
    print("My name:", name)
    print("My gender:", gender)

describe_person("Tuna", "M")
describe_person("Tanya", "F")
describe_person("John", "M")
My name: Tuna
My gender: M
My name: Tanya
My gender: F
My name: John
My gender: M

Without a function, you might write a separate pair of print() statements for every person. A reusable function keeps the behavior in one place and lets each call provide new data.

Required Positional Arguments

A parameter without a default value is a required positional argument parameter. It must receive a value when the function is called.

def describe_person(name, gender):
    print("My name:", name)
    print("My gender:", gender)

describe_person("Tuna")

This call supplies a value for name, but no value for gender. Python raises a TypeError before the function body runs. The exact message commonly looks like this:

TypeError: describe_person() missing 1 required positional argument: 'gender'

To diagnose this error, compare the function signature with the call. The signature has two parameters, name and gender, while the call has only one argument.

Argument Count Requirements

A normal function call must provide a value for every required parameter. It must also avoid supplying more positional arguments than the function accepts.

For example, this call has too many arguments:

def describe_person(name, gender):
    print("My name:", name)
    print("My gender:", gender)

describe_person("Tuna", "M", 25)

The function declares two parameters but receives three arguments, so Python raises a TypeError similar to:

TypeError: describe_person() takes 2 positional arguments but 3 were given

Some functions are deliberately designed to accept extra positional values with a variadic parameter such as *args. The function above is not designed that way, so the extra value is invalid.

Default parameter values can make some arguments optional:

def describe_person(name, gender="unknown"):
    print("My name:", name)
    print("My gender:", gender)

describe_person("Tuna")

Here, name is still required, but gender uses "unknown" when the call does not provide a second argument. See default values for parameters for more practice.

Why Argument Order Matters

Positional matching is based on location, not on the meaning of a value. Reversing two arguments may not cause an error if the number of arguments is still correct.

def describe_person(name, gender):
    print("My name:", name)
    print("My gender:", gender)

describe_person("M", "Tuna")
My name: M
My gender: Tuna

Python accepts this call because it has two arguments for two parameters. However, the values are assigned incorrectly: "M" becomes name, and "Tuna" becomes gender. Positional calls therefore require the caller to know and follow the parameter order.

Choose a clear parameter order when defining a function. When a call contains several values or the meaning is not obvious, keyword arguments can make the call clearer:

describe_person(name="Tuna", gender="M")

Keyword arguments identify parameters by name rather than relying only on position.

Common Positional Call Outcomes

Function callArgument count/orderOutcomeReason
describe_person("Tuna", "M")Correct count and orderExpected outputValues bind to matching parameter positions
describe_person("Tuna")One required argument missingTypeErrorgender has no supplied value
describe_person("M", "Tuna")Correct count, reversed meaningRuns but produces incorrect labelsBinding is positional rather than semantic
describe_person("Tuna", "M", 25)One extra argumentTypeErrorThe function declares only two parameters

Troubleshooting Positional Argument Errors

<

Missing required positional argument

Symptom: The traceback reports a missing required positional argument.

Likely cause: The call supplies fewer arguments than required by parameters without default values.

Diagnosis: Count the required parameters in the function signature and compare them with the values in the call.

Fix: Supply the missing argument, or add a default value if that parameter should genuinely be optional.

Values appear in the wrong fields

Symptom: The function runs, but the printed values appear under the wrong labels.

Likely cause: Arguments were supplied in the wrong positional order.

Diagnosis: Compare each argument's position with the parameter order in the function definition.

Fix: Reorder the arguments or use keyword arguments when readability is important.

Too many positional arguments

Symptom: Python reports that too many positional arguments were given.

Likely cause: The call includes more values than the function's declared parameters accept.

Diagnosis: Inspect the active function signature and count its parameters.

Fix: Remove the extra argument, add an appropriate parameter, or use a variadic positional parameter only when accepting arbitrary extra values is intended.

The traceback names an unexpected parameter

Symptom: The error message names a parameter that does not match the example you are reviewing.

Likely cause: The executed function definition has a different parameter name or a different version of the code.

Diagnosis: Verify that the saved file, executed code, function signature, and traceback refer to the same function definition.

Fix: Update the call to match the active definition and run the correct file again. For background on reading Python failures, see types of errors and syntax and logical errors.

Exam-Ready Summary

  • A parameter is a named variable declared in a function definition.
  • An argument is a value supplied in a function call.
  • A positional argument is matched by its location.
  • Python assigns positional arguments from left to right.
  • The first argument goes to the first parameter, the second goes to the second parameter, and so on.
  • Parameters without default values are required.
  • Missing required arguments and extra arguments normally raise TypeError.
  • Correct argument count does not guarantee correct meaning; reversed values can produce incorrect output without an exception.
  • Each function call creates parameter bindings for that particular execution.
  • Use keyword arguments when a positional call is difficult to read.