Parameter and Arguments

M3-R5.1 · Chapter 6: Functions · 4 min read

7. Parameters and Arguments

Term

Definition

Where it appears

Example

Parameter (Formal Parameter)

Variable listed in the function definition header; acts as a placeholder

Inside def statement

def calcSum(x, y):  → x and y are parameters

Argument (Actual Parameter)

Actual value passed to the function when calling it

Inside function call

calcSum(5, 10)      → 5 and 10 are arguments

def calcSum(x, y):          # x, y are PARAMETERS

    s = x + y

print(calcSum(2, 3))        # 2, 3 are ARGUMENTS  → Output: 5

a = 5

b = 6

print(calcSum(a, b))        # a, b are ARGUMENTS  → Output: 11

d = 10

print(calcSum(9, d))        # 9, d are ARGUMENTS  → Output: 19

8. Types of Function Arguments

Python supports four types of formal arguments / parameters:

8.1 Positional Arguments (Required Arguments)

The function call must match the number and order of arguments exactly as defined in the function definition.

def check(a, b, c):

    print(a, b, c)

check(1, 2, 3)          # Correct: 3 positional args → Output: 1 2 3

# check(1, 2)           # Error: missing 1 required argument

8.2 Keyword (Named) Arguments

Arguments are passed with their parameter name explicitly mentioned. Order does not matter when keyword arguments are used.

def interest(prin, time, rate):

    return prin * time * rate

# Order doesn't matter when using keywords

print(interest(prin=2000, time=2, rate=0.10))    # Output: 400.0

print(interest(time=4, prin=2600, rate=0.09))    # Output: 936.0

print(interest(time=2, rate=0.12, prin=2000))    # Output: 480.0

8.3 Default Arguments

A default value is given to a parameter in the function definition. If the caller does not supply a value for that parameter, the default is used.

def greet(name, message='Hello'):

    print(message + ',', name)

greet('Alice')                   # Output: Hello, Alice  (default used)

greet('Bob', 'Good Morning')     # Output: Good Morning, Bob

# Another example

def power(base, exp=2):          # exp defaults to 2 (square)

    return base ** exp

print(power(3))      # Output: 9   (3^2)

print(power(2, 10))  # Output: 1024 (2^10)

📝  NOTE: Default arguments must always come after non-default arguments in the function definition. def func(a=1, b) is INVALID.

8.4 Variable-length Arguments (*args and **kwargs)

When you don't know in advance how many arguments will be passed, use *args (for positional) or **kwargs (for keyword arguments).

# *args – collects extra positional arguments as a tuple

def add_all(*numbers):

    total = 0

    for n in numbers:

        total += n

    return total

print(add_all(1, 2))          # Output: 3

print(add_all(1, 2, 3, 4, 5)) # Output: 15

# **kwargs – collects extra keyword arguments as a dictionary

def display_info(**details):

    for key, value in details.items():

        print(key, ':', value)

display_info(name='Alice', age=21, city='Delhi')

# Output:

# name : Alice

# age  : 21

# city : Delhi