Lesson 35 of 70 – Python Function Arguments
50%

Python Function Arguments

Function arguments are the values that are passed to a function when the function is called. Arguments allow the same function to work with different data.

Note: A function can accept one or more arguments. Python supports positional arguments, keyword arguments, default arguments, and arbitrary arguments.
1. What is a Function Argument?

An argument is a value passed to a function when calling it.

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

greet("Amit")
Output:
Hello Amit

Here, name is the parameter and "Amit" is the argument.

2. Parameter vs Argument
Term Meaning
Parameter Variable written in the function definition
Argument Actual value passed to the function
def add(a, b):
    return a + b

add(10, 20)

Here a and b are parameters, while 10 and 20 are arguments.

3. Positional Arguments

Positional arguments are matched with parameters according to their position.

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

student("Rahul", 21)
Output:
Name: Rahul
Age: 21
4. Order Matters in Positional Arguments

The order of positional arguments is important.

def student(name, age):
    print(name)
    print(age)

student("Amit", 20)
Output:
Amit
20

If the values are passed in the wrong order, the function may receive unexpected values.

5. Multiple Positional Arguments
def add(a, b, c):
    return a + b + c

result = add(10, 20, 30)

print(result)
Output:
60
6. Keyword Arguments

Keyword arguments are passed using the parameter name.

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

student(age=21, name="Priya")
Output:
Name: Priya
Age: 21

The order of keyword arguments does not need to match the parameter order.

7. Positional and Keyword Arguments Together

You can combine positional and keyword arguments, but positional arguments must come before keyword arguments.

def student(name, age, course):
    print(name, age, course)

student("Amit", age=20, course="Python")
Output:
Amit 20 Python
8. Invalid Argument Order

A positional argument cannot normally be placed after a keyword argument.

def student(name, age):
    print(name, age)

student(name="Amit", 20)
Result:
SyntaxError
9. Default Arguments

A parameter can have a default value. If the caller does not provide a value, Python uses the default.

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

greet()
greet("Amit")
Output:
Hello Student
Hello Amit
10. Multiple Default Arguments
def student(name="Unknown", course="Python"):
    print("Name:", name)
    print("Course:", course)

student()
Output:
Name: Unknown
Course: Python
11. Overriding a Default Argument

A default value can be replaced by providing an argument.

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

greet("Rahul")
Output:
Hello Rahul
12. Required Arguments

A parameter without a default value is required unless the function is designed to receive it through another mechanism.

def add(a, b):
    return a + b

print(add(10, 20))
Output:
30

Calling add() without the required arguments causes a TypeError.

13. Missing Required Argument
def add(a, b):
    return a + b

add(10)
Result:
TypeError: missing 1 required positional argument
14. Arbitrary Positional Arguments – *args

Use *args when you want a function to accept a variable number of positional arguments.

def add(*numbers):
    print(numbers)

add(10, 20, 30, 40)
Output:
(10, 20, 30, 40)

Inside the function, args is a tuple containing the positional arguments.

15. Using *args for Addition
def add(*numbers):

    total = 0

    for number in numbers:
        total += number

    return total

print(add(10, 20))
print(add(10, 20, 30))
print(add(1, 2, 3, 4, 5))
Output:
30
60
15
16. Arbitrary Keyword Arguments – **kwargs

Use **kwargs when you want a function to accept a variable number of keyword arguments.

def student(**details):
    print(details)

student(name="Amit", age=20, course="Python")
Output:
{'name': 'Amit', 'age': 20, 'course': 'Python'}

Inside the function, kwargs is a dictionary.

17. Looping Through **kwargs
def student(**details):

    for key, value in details.items():
        print(key, ":", value)

student(name="Rahul", age=21, city="Patna")
Output:
name : Rahul
age : 21
city : Patna
18. Combining Regular Parameters and *args

A regular parameter can be used before *args.

def student(name, *subjects):

    print("Name:", name)

    for subject in subjects:
        print("Subject:", subject)

student("Amit", "Python", "SQL", "HTML")
Output:
Name: Amit
Subject: Python
Subject: SQL
Subject: HTML
19. Combining *args and **kwargs
def student(*subjects, **details):

    print("Subjects:", subjects)
    print("Details:", details)

student(
    "Python",
    "SQL",
    name="Amit",
    age=20
)
Output:
Subjects: ('Python', 'SQL')
Details: {'name': 'Amit', 'age': 20}
20. Unpacking a List into Arguments

The * operator can unpack the elements of a list or tuple into positional arguments.

def add(a, b, c):
    return a + b + c

numbers = [10, 20, 30]

print(add(*numbers))
Output:
60
21. Unpacking a Dictionary into Keyword Arguments

The ** operator can unpack a dictionary into keyword arguments.

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

data = {
    "name": "Priya",
    "age": 22
}

student(**data)
Output:
Name: Priya
Age: 22
22. Keyword-Only Arguments

Parameters placed after * must be passed using their parameter names.

def student(name, *, age):
    print(name, age)

student("Amit", age=20)
Output:
Amit 20

Here, age is a keyword-only argument.

23. Positional-Only Arguments

Parameters placed before / can be defined as positional-only parameters.

def add(a, b, /):
    return a + b

print(add(10, 20))
Output:
30

The parameters before / cannot be passed using keyword syntax.

24. Argument Order in a Function

A function definition can use different types of parameters, but they must follow Python's parameter ordering rules.

def example(a, b=10, *args, **kwargs):
    print(a)
    print(b)
    print(args)
    print(kwargs)

example(1, 20, 30, 40, name="Amit")
Output:
1
20
(30, 40)
{'name': 'Amit'}
25. Practical Example – Calculate Total
def calculate_total(*prices):

    total = 0

    for price in prices:
        total += price

    return total

print("Total:", calculate_total(100, 200, 150))
Output:
Total: 450
26. Key Points
  • An argument is a value passed to a function.
  • A parameter is a variable defined in the function.
  • Positional arguments are matched according to position.
  • Keyword arguments are passed using parameter names.
  • Positional arguments must come before keyword arguments in a function call.
  • Default arguments provide fallback values.
  • *args accepts a variable number of positional arguments.
  • **kwargs accepts a variable number of keyword arguments.
  • The * operator can unpack iterables into positional arguments.
  • The ** operator can unpack dictionaries into keyword arguments.
  • Parameters after * can be keyword-only.
  • Parameters before / can be positional-only.

🧠 Quick Quiz

Question: Which syntax is used to accept a variable number of positional arguments?