Concepts / Function Definition and Parameters

Function Definition and Parameters

When you use default values, parameters with defaults must come after parameters without defaults in the function definition. You cannot have a required parameter after an optional one.

  • Programming
Interactive lab

Try it: Binding Arguments to Parameters

How Python matches a call's arguments to a function's parameters — positionals left to right, extras into *args, keywords by name, defaults for the rest, unknown names into **kwargs — and the exact SyntaxError or TypeError when a call doesn't fit, including the shared mutable default.

How it works

  1. The parser checks the call first: a positional after a keyword, or a repeated keyword, is a SyntaxError before anything runs.
  2. At the call site, *list spreads into separate positional arguments and **dict into keyword arguments.
  3. Positional arguments fill the parameters left to right; leftovers go into the *args tuple.
  4. Each keyword matches a parameter by exact name (a second value is 'got multiple values'); unknown names go into **kwargs or raise 'unexpected keyword argument'.
  5. Then: too many positionals, or a required parameter still empty, is a TypeError; otherwise defaults fill the rest.
  6. Defaults are evaluated once, at def time: a list default is the same object in every call.

Default run (13 steps): def greet(…) runs once: it creates the function and evaluates its default values right now. Then the call greet('Ann', 'Hi', *[7, 8], sep='-', mood='happy') starts. … Done. Printed: {'name': 'Ann', 'greeting': 'Hi', 'sep': '-', 'args': (7, 8), 'kwargs': {'mood': 'happy'}}.

Simplified: Five hand-written signatures with no positional-only (/) parameters; argument values are small literals (ints, short strs, True/False/None, flat lists). Error messages are CPython 3.12's (3.13+ may add a 'Did you mean' hint).

Educational simulation

Loading the simulation…

What Are Parameters?

When you define a function, you often want it to work with different data each time you call it. Parameters are the named placeholders inside a function definition that let you do this. They act like variables that will receive values when the function is called. Think of a parameter as a slot waiting to be filled with a specific value.

Parameters are the names you write inside the parentheses when you define a function. They specify what information the function expects to receive. When you actually call the function and provide values, those values are called arguments. This distinction matters: parameters are part of the definition; arguments are what you pass when you call the function.

Parameters vs Arguments: The Terminology

Definition

Parameter: a name declared in the function definition, inside the parentheses, that represents a value the function will receive.

Definition

Argument: the actual value you supply when you call the function, matched to a parameter by position or name.

def greet(name,greeting):Function definitionnameParametergreet('Alice','Hello')Function call'Alice'ArgumentgreetingParameter'Hello'Argumentreceivesreceives
How do parameter names in the function definition connect to the argument values you pass when calling the function?

How Parameters Are Specified

Parameters are written inside the parentheses that follow the function name in the definition. If a function has multiple parameters, you separate them with commas. The order matters: the first argument you pass will be assigned to the first parameter, the second argument to the second parameter, and so on.

defKeywordcalculate_totalFunction name(Opens parameter listpriceFirst parameter,SeparatorquantitySecond parameter)Closes parameter list:Starts function body
What does the structure of parameters inside the function definition look like?

Matching Arguments to Parameters by Position

When you call a function, Python matches each argument to a parameter based on the order they appear. The first argument goes to the first parameter, the second to the second, and so on. This is called positional argument passing.

Worked example

Positional Arguments

You have a function definition: def introduce(name, age, city):. You call it as introduce('Maya', 28, 'Boston'). Which argument gets assigned to which parameter?

  1. 1
    Identify parameters in order

    The function definition has three parameters in this order: name, age, city.

  2. 2
    Identify arguments in order

    The function call provides three arguments in this order: 'Maya', 28, 'Boston'.

  3. 3
    Match by position

    First argument 'Maya' goes to first parameter name. Second argument 28 goes to second parameter age. Third argument 'Boston' goes to third parameter city.

  4. 4
    Inside the function

    When the function body runs, name has the value 'Maya', age has the value 28, and city has the value 'Boston'.

Result

name = 'Maya', age = 28, city = 'Boston'

Function DefinitionnamePosition 0Function Call'Maya'Position 0agePosition 128Position 1cityPosition 2'Boston'Position 2matchesmatchesmatches
How do the positions of values in a function call match up with the parameter names in the function definition?

Default Parameter Values

Sometimes you want a parameter to have a default value that the function will use if the caller does not provide an argument for it. This makes a parameter optional. You specify a default value by appending an equals sign and the value to the parameter name in the function definition.

Key point

When you use default values, parameters with defaults must come after parameters without defaults in the function definition. You cannot have a required parameter after an optional one.

Worked example

Function with Default Parameters

Write a function definition for send_message that takes a recipient (required) and a message (optional, defaulting to 'Hello'). Then show what happens when you call it both with and without the second argument.

  1. 1
    Define the function

    def send_message(recipient, message='Hello'): — recipient has no default, so it is required; message has a default value of 'Hello', so it is optional.

  2. 2
    Call with both arguments

    send_message('Alice', 'Good morning') — recipient becomes 'Alice', message becomes 'Good morning' (the default is overridden).

  3. 3
    Call with only the required argument

    send_message('Bob') — recipient becomes 'Bob', message uses its default value 'Hello' because no second argument was provided.

Result

The function can be called in two ways: with the default message or with a custom message.

Keyword Arguments

In addition to passing arguments by position, you can pass them by name using keyword arguments. When you use a keyword argument, you write the parameter name, an equals sign, and the value. This allows you to pass arguments in any order, and it makes your code more readable because the parameter name is explicit.

Worked example

Keyword Arguments

A function is defined as func(a, b, c). Show three different ways to call it: all positional, all keyword, and mixed.

  1. 1
    All positional arguments

    func(10, 20, 30) — arguments are matched to parameters by position: a=10, b=20, c=30.

  2. 2
    All keyword arguments

    func(c=50, a=100, b=75) — arguments are matched by name, so order does not matter: a=100, b=75, c=50.

  3. 3
    Mixed positional and keyword

    func(10, c=30, b=20) — positional arguments are matched first (a=10), then keyword arguments are matched by name (b=20, c=30).

Result

Keyword arguments give you flexibility in the order you pass values and make the intent of each argument clear.

Common Mistakes with Parameters

  • Common mistake

    Forgetting to separate multiple parameters with commas

    What it looks like
    def add(a b): — missing comma between a and b
    Why it fails

    Python interprets this as a syntax error. The comma is required to tell Python where one parameter name ends and the next begins.

    Do this instead
    def add(a, b):
  • Common mistake

    Confusing the number of arguments with the number of parameters

    What it looks like
    def greet(name): greet('Alice', 'Bob') — function expects 1 parameter but 2 arguments are passed
    Why it fails

    Python will raise a TypeError because the function received more arguments than it has parameters to accept.

    Do this instead

    Either define the function with two parameters (def greet(name1, name2):) or call it with one argument (greet('Alice')).

  • Common mistake

    Placing a parameter with a default value before one without a default

    What it looks like
    def process(x=10, y): — x has a default but y does not
    Why it fails

    Python will raise a SyntaxError. Required parameters must come before optional ones.

    Do this instead
    def process(y, x=10):
  • Common mistake

    Using a positional argument after a keyword argument

    What it looks like

    func(a=5, 10) — keyword argument a=5 comes before positional argument 10

    Why it fails

    Python will raise a SyntaxError. Positional arguments must always come before keyword arguments in a function call.

    Do this instead
    func(10, a=5)

Parameters as a Template

Think of a function definition with parameters as a template. The parameters are placeholders that get filled with different values each time you call the function. This is what makes functions powerful: you write the logic once, and it works with many different inputs. Each call to the function creates a new execution with its own set of parameter values, independent of previous calls.

In the real world

Imagine a recipe for making tea. The recipe (function definition) has parameters like 'water temperature' and 'steeping time'. Each time you make tea (call the function), you might use different values: one time 200 degrees for 3 minutes, another time 180 degrees for 5 minutes. The recipe stays the same, but the parameters adapt to your needs each time.

Practice

MEDIUM

Write a function definition for calculate_discount that takes two parameters: original_price (required) and discount_percent (optional, defaulting to 10). Then write three different function calls: one using only the required parameter, one using both parameters positionally, and one using both parameters as keywords.

Hints
  • Remember to separate parameters with commas in the definition.
  • For the default parameter, use the syntax parameter_name=default_value.
  • For keyword arguments in the call, use parameter_name=value.

Key Takeaways

  • Parameters are named placeholders in a function definition that receive values when the function is called. Arguments are the actual values you pass.
  • Parameters are separated by commas inside the parentheses in the function definition, and arguments are matched to parameters by position or by name.
  • Default parameter values make a parameter optional; if the caller does not provide an argument, the default value is used.
  • Keyword arguments let you pass values by name, allowing any order, but positional arguments must come before keyword arguments in a function call.
  • A function definition with parameters acts as a reusable template that can accept different values each time it is called.

Related concepts

More concepts starting with F