Concepts / Function Definition and Parameters

Function Definition and Parameters

Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the function, we supply the values in the same way. Note the terminology used - the names given in the function definition are called parameters whereas the values you supply in the function call are called arguments .

  • Programming

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

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

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

receivesreceivesdef greet(name,greeting):Function definitionnameParametergreet('Alice','Hello')Function call'Alice'ArgumentgreetingParameter'Hello'Argument
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.

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?

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

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

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.

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

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

matchesmatchesmatchesFunction DefinitionnamePosition 0Function Call'Maya'Position 0agePosition 128Position 1cityPosition 2'Boston'Position 2
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.

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.

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.

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.

Call with both arguments: send_message('Alice', 'Good morning') — recipient becomes 'Alice', message becomes 'Good morning' (the default is overridden).

Call with only the required argument: send_message('Bob') — recipient becomes 'Bob', message uses its default value 'Hello' because no second argument was provided.

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.

Keyword Arguments

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

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

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.

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

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

Common Mistakes with Parameters

  • Forgetting to separate multiple parameters with commas

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

    Fix: def add(a, b):

  • Confusing the number of arguments with the number of parameters

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

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

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

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

    Fix: def process(y, x=10):

  • Using a positional argument after a keyword argument

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

    Fix: 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.

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.