Parameters and Arguments in Python: Syntax, Rules and Examples

Introduction: Parameters and Arguments in Python

Functions often need values to work with. For example, a function that calculates a total may need a price and quantity, while a function that displays a message may need a name. Parameters and Arguments in Python allow functions to receive these values.

So, how does a Python function receive these values?

Like other programming languages, Python provides parameters and arguments to handle values passed to functions. These features allow the same function to work with different values each time it is called.

But what exactly are parameters and arguments and what is the difference between them?

The sections below explain these concepts, the difference between them and how they are used in Python functions.

What Is a Parameter?

Definition: A parameter is a variable written inside the parentheses of a function definition. It acts as a placeholder for a value that the function will receive when it is called.

Example: Function With a Parameter

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

Explanation: Here, name is a parameter. It allows greet() to work with different names.

What Is an Argument?

An argument is the value passed to a function when the function is called.

Example: Passing an Argument

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

greet("Alice")

# Output:
Hello, Alice

Explanation: Here, "Alice" is the argument passed to greet(). Python assigns this value to the name parameter when the function is called.

Parameters vs Arguments

The main difference is where they appear. A parameter is written in the function definition, while an argument is the value passed when the function is called.

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

greet("Alice")
Parameter Argument
name "Alice"
Written in the function definition Passed when the function is called
Acts as a placeholder Provides the actual value

In short:

  • The parameter is the placeholder that receives a value.
  • The argument is the value passed to the parameter.

Syntax of Parameters and Arguments

Parameters are written inside the parentheses of a function definition, while arguments are written inside the parentheses when the function is called.

def function_name(parameter1, parameter2):
    # function body

function_name(argument1, argument2)

In this example, parameter1 and parameter2 are parameters defined by the function, while argument1 and argument2 are the values passed to those parameters when the function is called.

Types of Arguments in Python: Parameters and Arguments in Python

Parameters and Arguments in Python allow functions to receive values in different ways. The way you pass an argument determines how Python matches it with a function parameter.

The main topics covered here are positional arguments, keyword arguments, default values for parameters, and using positional and keyword arguments together.

A) Positional Arguments

B) Keyword Arguments

C) Default Values for Parameters

D) Positional and Keyword Arguments Together

A) Positional Arguments

Positional arguments are matched with parameters according to their position in the function call.

Example

def introduce(name, age):
    print("Name:", name)
    print("Age:", age)

introduce("Alice", 25)


# Output:
Name: Alice
Age: 25

Explanation: The first argument, "Alice", is assigned to name. The second argument, 25, is assigned to age.

The order of positional arguments matters. Changing their order changes which parameter receives each value.

↑ Move to Section Top

B) Keyword Arguments

Keyword arguments are passed by writing the parameter name followed by an equals sign and the value.

Example

def introduce(name, age):
    print("Name:", name)
    print("Age:", age)

introduce(age=25, name="Alice")


# Output:
Name: Alice
Age: 25

Explanation: Here, name="Alice" assigns "Alice" to name, and age=25 assigns 25 to age.

The order of keyword arguments does not matter because each value is matched using its parameter name.

↑ Move to Section Top

C) Default Values for Parameters

A parameter can have a default value specified in the function definition. If no argument is passed for that parameter, Python uses the default value.

Example

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

greet()


# Output:
Hello, Guest

Explanation: The parameter name has the default value "Guest". Since no argument is passed in the function call, Python uses that value.

A different value can still be passed when needed.

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

greet("Alice")


# Output:
Hello, Alice

Explanation: Here, "Alice" is passed as an argument, so Python uses it instead of the default value.

↑ Move to Section Top

D) Positional and Keyword Arguments Together

A function call can use positional and keyword arguments together. When both are used, positional arguments must come before keyword arguments.

Example

def introduce(name, age):
    print("Name:", name)
    print("Age:", age)

introduce("Alice", age=25)


# Output:
Name: Alice
Age: 25

Explanation: Here, "Alice" is passed as a positional argument and age=25 is passed as a keyword argument. The positional argument is matched with name, while the keyword argument is matched with age.

Incorrect Order of Arguments

A keyword argument cannot come before a positional argument in the same function call.

def introduce(name, age):
    print("Name:", name)
    print("Age:", age)

introduce(name="Alice", 25)


# Error:
SyntaxError: positional argument follows keyword argument

Explanation: Here, name="Alice" is a keyword argument, but 25 is a positional argument. Python does not allow a positional argument to appear after a keyword argument, so the function call raises a SyntaxError.

↑ Move to Section Top

Examples: Parameters and Arguments in Python

These examples show how parameters receive arguments and how functions use the values passed to them.

  1. Using Parameters and Arguments
  2. Using Multiple Parameters and Arguments
  3. Using a Default Argument

1. Using Parameters and Arguments

A function can receive values through parameters when arguments are passed in a function call.

def calculate_total(price, quantity):
    total = price * quantity
    print(total)

calculate_total(50, 3)


# Output:
150

Explanation: Here, price and quantity are parameters. The values 50 and 3 are arguments passed to the function. Python assigns these arguments to the corresponding parameters, and the function uses them to calculate the total.

↑ Move to Section Top

2. Using Multiple Parameters and Arguments

A function can have several parameters and receive a corresponding argument for each one.

def calculate_average(a, b, c):
    average = (a + b + c) / 3
    print(average)

calculate_average(80, 90, 70)


# Output:
80.0

Explanation: The parameters a, b, and c receive the arguments 80, 90, and 70. The function uses these values to calculate the average.

↑ Move to Section Top

3. Using a Default Argument

A parameter can have a default value. If an argument is not provided for that parameter, Python uses the default value.

def calculate_total(price, quantity=1):
    total = price * quantity
    print(total)

calculate_total(100)
calculate_total(100, 3)


# Output:
100
300

Explanation: The quantity parameter has a default value of 1.

  • In the first call, no argument is provided for quantity, so Python uses 1.
  • In the second call, 3 is passed as an argument, so Python uses 3 instead of the default value.

↑ Move to Section Top

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top