What is another term for parameters of a function when it is called?

This article explains Python’s various function arguments with clear examples of how to use them. But before learning all function arguments in detail, first, understand the use of argument or parameter in the function.

Also, See

  • Python Functions Exercise
  • Python Functions Quiz

Table of contents

What is a function argument?

When we define and call a Python function, the term parameter and argument is used to pass information to the function.

  • parameter: It is the variable listed inside the parentheses in the function definition.
  • argument: It is a value sent to the function when it is called. It is data on which function performs some action and returns the result.

Example:

In this example, the function

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
2 is defined with three parameters,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
3,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
4,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
5, and print the sum of all three values of the arguments passed during a function call.

# a, b, c are arguments of the function
def my_sum(a, b, c):
    s = a + b + c
    return s

print('Total is:', my_sum(30, 40, 50))

Output:

Total is: 120

What is another term for parameters of a function when it is called?
What is another term for parameters of a function when it is called?
function argument and parameter

Note: A function must be called with the correct number of arguments by default. For example, the above function expects 3 arguments, so you have to call the

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
6 function with 3 arguments; otherwise, you will get an error.

Does function need arguments?

It is not mandatory to use arguments in function definition. But if you need to process user data, you need arguments in the function definition to accept that data.

Also, we use argument in function definition when we need to perform the same task multiple times with different data.

Can a function be called without arguments?

If the function is defined with parameters, the arguments passed must match one of the arguments the function accepts when calling.

Types of function arguments

There are various ways to use arguments in a function. In Python, we have the following 4 types of function arguments.

  1. Default argument
  2. Keyword arguments (named arguments)
  3. Positional arguments
  4. Arbitrary arguments (variable-length arguments
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    7 and
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    8)

What is another term for parameters of a function when it is called?
What is another term for parameters of a function when it is called?
Types of Python function arguments explained with examples

Default Arguments

In a function, arguments can have default values. We assign default values to the argument using the ‘=’ (assignment) operator at the time of function definition. You can define a function with any number of default arguments.

The default value of an argument will be used inside a function if we do not pass a value to that argument at the time of the function call. Due to this, the default arguments become optional during the function call.

It overrides the default value if we provide a value to the default arguments during function calls.

Let us understand this with an example.

Example:

Let’s define a function

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
9 with four arguments
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
0,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
1,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
2, and
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
3. In this function,
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
2 and
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
3 are default arguments with default values.

  • If you call a function without
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # not passing a school value (default value is used)
    # six is assigned to grade
    student('Kelly', 12, 'Six')
    
    # passign all arguments
    student('Jessa', 12, 'Seven', 'XYZ School')
    3 and
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # not passing a school value (default value is used)
    # six is assigned to grade
    student('Kelly', 12, 'Six')
    
    # passign all arguments
    student('Jessa', 12, 'Seven', 'XYZ School')
    2 arguments, then the default values of
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # not passing a school value (default value is used)
    # six is assigned to grade
    student('Kelly', 12, 'Six')
    
    # passign all arguments
    student('Jessa', 12, 'Seven', 'XYZ School')
    2 and
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # not passing a school value (default value is used)
    # six is assigned to grade
    student('Kelly', 12, 'Six')
    
    # passign all arguments
    student('Jessa', 12, 'Seven', 'XYZ School')
    3 are used.
  • The
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # not passing a school value (default value is used)
    # six is assigned to grade
    student('Kelly', 12, 'Six')
    
    # passign all arguments
    student('Jessa', 12, 'Seven', 'XYZ School')
    1 and
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # not passing a school value (default value is used)
    # six is assigned to grade
    student('Kelly', 12, 'Six')
    
    # passign all arguments
    student('Jessa', 12, 'Seven', 'XYZ School')
    0 parameters do not have default values and are required (mandatory) during a function call.
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School

Passing one of the default arguments:

If you pass values of the

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
2 and
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
3 arguments while calling a function, then those values are used instead of default values.

Example:

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')

Output:

Student Details: Kelly 12 Six ABC School
Student Details: Jessa 12 Seven XYZ School

Keyword Arguments

Usually, at the time of the function call, values get assigned to the arguments according to their position. So we must pass values in the same sequence defined in a function definition.

For example, when we call

Student Details: Kelly 12 Six ABC School
Student Details: Jessa 12 Seven XYZ School
4, the value “Jon” gets assigned to the argument name, and similarly, 12 to
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# not passing a school value (default value is used)
# six is assigned to grade
student('Kelly', 12, 'Six')

# passign all arguments
student('Jessa', 12, 'Seven', 'XYZ School')
1 and so on as per the sequence.

We can alter this behavior using a keyword argument.

Keyword arguments are those arguments where values get assigned to the arguments by their keyword (name) when the function is called. It is preceded by the variable name and an (

Student Details: Kelly 12 Six ABC School
Student Details: Jessa 12 Seven XYZ School
6) assignment operator. The Keyword Argument is also called a named argument.

Example:

# function with 2 keyword arguments
def student(name, age):
    print('Student Details:', name, age)

# default function call
student('Jessa', 14)

# both keyword arguments
student(name='Jon', age=12)

# 1 positional and 1 keyword
student('Donald', age=13)

Output:

Student Details: Jessa 14
Student Details: Jon 12
Student Details: Donald 13

Change the sequence of keyword arguments

Also, you can change the sequence of keyword arguments by using their name in function calls.

Python allows functions to be called using keyword arguments. But all the keyword arguments should match the parameters in the function definition. When we call functions in this way, the order (position) of the arguments can be changed.

Example:

# both keyword arguments by changing their order
student(age=13, name='Kelly')

# Output: Student Details: Kelly 13

Positional Arguments

Positional arguments are those arguments where values get assigned to the arguments by their position when the function is called. For example, the 1st positional argument must be 1st when the function is called. The 2nd positional argument needs to be 2nd when the function is called, etc.

By default, Python functions are called using the positional arguments.

Example: Program to subtract 2 numbers using positional arguments.

def add(a, b):
    print(a - b)

add(50, 10)
# Output 40

add(10, 50)
# Output -40

Note: If you try to pass more arguments, you will get an error.

def add(a, b):
    print(a - b)

add(105, 561, 4)

Output

Total is: 120
0

Note: In the positional argument number and position of arguments must be matched. If we change the order, then the result may change. Also, If we change the number of arguments, we will get an error.

Important points to remember about function argument

Point 1: Default arguments should follow non-default arguments

Example:

Total is: 120
1

Point: Default arguments must follow the default argument in a function definition

Default arguments must follow the default argument. For example, When you use the default argument in a definition, all the arguments to their right must also have default values. Otherwise, you’ll get an error.

Example:

Total is: 120
2

Point 2: keyword arguments should follow positional arguments only.

we can mix positional arguments with keyword arguments during a function call. But, a keyword argument must always be after a non-keyword argument (positional argument). Else, you’ll get an error.

I.e., avoid using keyword argument before positional argument.

Example:

Total is: 120
3

Point 3: The order of keyword arguments is not important, but All the keyword arguments passed must match one of the arguments accepted by the function.

Example:

Total is: 120
4

Point 4: No argument should receive a value more than once

Total is: 120
5

Variable-length arguments

In Python, sometimes, there is a situation where we need to pass multiple arguments to the function. Such types of arguments are called arbitrary arguments or variable-length arguments.

We use variable-length arguments if we don’t know the number of arguments needed for the function in advance.

Types of Arbitrary Arguments:

  • arbitrary positional arguments (
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    7)
  • arbitrary keyword arguments (
    # function with 2 keyword arguments grade and school
    def student(name, age, grade="Five", school="ABC School"):
        print('Student Details:', name, age, grade, school)
    
    # without passing grade and school
    # Passing only the mandatory arguments
    student('Jon', 12)
    
    # Output: Student Details: Jon 12 Five ABC School
    8)

The *args and **kwargs allow you to pass multiple positional arguments or keyword arguments to a function.

Arbitrary positional arguments (# function with 2 keyword arguments grade and school def student(name, age, grade="Five", school="ABC School"): print('Student Details:', name, age, grade, school) # without passing grade and school # Passing only the mandatory arguments student('Jon', 12) # Output: Student Details: Jon 12 Five ABC School7)

We can declare a variable-length argument with the

# function with 2 keyword arguments
def student(name, age):
    print('Student Details:', name, age)

# default function call
student('Jessa', 14)

# both keyword arguments
student(name='Jon', age=12)

# 1 positional and 1 keyword
student('Donald', age=13)
0 (asterisk) symbol. Place an asterisk (
# function with 2 keyword arguments
def student(name, age):
    print('Student Details:', name, age)

# default function call
student('Jessa', 14)

# both keyword arguments
student(name='Jon', age=12)

# 1 positional and 1 keyword
student('Donald', age=13)
0) before a parameter in the function definition to define an arbitrary positional argument.

we can pass multiple arguments to the function. Internally all these values are represented in the form of a tuple. Let’s understand the use of variable-length arguments with an example.

This is a simple function that takes three arguments and returns their average:

Total is: 120
6

This function works, but it’s limited to only three arguments. What if you need to calculate the average marks of more than three subjects or the number of subjects is determined only at runtime? In such cases, it is advisable to use the variable-length of positional arguments to write a function that could calculate the average of all subjects no matter how many there are.

Example:

Total is: 120
7

Output:

Total is: 120
8

Note: args is just a name. You can choose any name that you prefer, such as *subjects.

Total is: 120
9

Arbitrary keyword arguments (**kwargs)

We saw how to use

# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
7. Now let’s see how to use the
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
8 argument. The **kwargs allow you to pass multiple keyword arguments to a function. Use the
# function with 2 keyword arguments grade and school
def student(name, age, grade="Five", school="ABC School"):
    print('Student Details:', name, age, grade, school)

# without passing grade and school
# Passing only the mandatory arguments
student('Jon', 12)

# Output: Student Details: Jon 12 Five ABC School
8 if you want to handle named arguments in a function.

Use the unpacking operator(

# function with 2 keyword arguments
def student(name, age):
    print('Student Details:', name, age)

# default function call
student('Jessa', 14)

# both keyword arguments
student(name='Jon', age=12)

# 1 positional and 1 keyword
student('Donald', age=13)
5) to define variable-length keyword arguments. Keyword arguments passed to a kwargs are accessed using key-value pair (same as accessing a dictionary in Python).

What is a parameter when it comes to functions?

A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: function example(parameter) { console.

What type of parameters are used in function call?

Actual parameters are the parameters appearing in the function call statement.