Function Arguments in Python – Types, Syntax & Examples

Introduction

Functions are one of the most powerful features in Python because they allow developers to organize code into reusable blocks. However, a function becomes even more useful when it can accept input values and process them dynamically. These input values are known as function arguments.

Function arguments enable a function to work with different data without rewriting the function code. Instead of creating separate functions for different values, you can pass values as arguments and let the function handle them accordingly.

What are Function Arguments in Python?

Function arguments are values passed to a function when it is called.

These values are received by the function through variables called parameters.

Example:


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

Output:

Hello John

In this example:

  • name is a parameter.
  • “John” is an argument.
  • The function receives the argument and displays it.

Why Use Function Arguments?

Without arguments:


def greet():
    print("Hello John")

This function can only greet John.

With arguments:


def greet(name):
    print("Hello", name)
greet("John")
greet("Emma")
greet("David")

Now the function can greet anyone.

Output:

Hello John
Hello Emma
Hello David

Arguments make functions reusable and flexible.

Syntax

The basic syntax is:


def function_name(parameter):
    # code block
function_name(argument)

Example:


def square(number):
    print(number * number)
square(5)

Output:

25

Parameter vs Argument

Many beginners confuse parameters and arguments.

Feature Parameter Argument
Definition Variable in function definition Value passed to function
Role Receives data Sends data
Usage Defined inside function declaration Used during function call

Example:


def greet(name):
    print(name)
greet("John")

Here:

  • name → Parameter
  • “John” → Argument

Types of Function Arguments in Python

Python supports several types of arguments:

  1. Positional Arguments
  2. Keyword Arguments
  3. Default Arguments
  4. Variable-Length Arguments (*args)
  5. Keyword Variable-Length Arguments (**kwargs)

Let’s understand each type.

1. Positional Arguments

Positional arguments are assigned according to their position.

Example:


def student(name, age):
    print(name, age)
student("John", 20)

Output:

John 20

The first value is assigned to name, and the second value is assigned to age.

Incorrect Positional Order


def student(name, age):
    print(name, age)
student(20, "John")

Output:

20 John

The values are assigned based on position, not variable names.

Multiple Positional Arguments

Example:


def add(a, b, c):
    print(a + b + c)
add(10, 20, 30)

Output:

60

2. Keyword Arguments

Keyword arguments allow you to specify parameter names when passing values.

Example:


def employee(name, age):
    print(name, age)
employee(age=25, name="Emma")

Output:

Emma 25

The order doesn’t matter because parameter names are specified.

Benefits of Keyword Arguments

Improved Readability


calculate_salary(
    hours=40,
    rate=20
)

This is easier to understand than:


calculate_salary(40, 20)

3. Default Arguments

Default arguments have predefined values.

Example:


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

Output:

Hello Guest

If no argument is supplied, the default value is used.

Overriding Default Values


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

Output:

Hello John

The provided argument overrides the default value.

Multiple Default Arguments


def employee(name="Unknown", age=18):
    print(name, age)
employee()

Output:

Unknown 18

4. Variable-Length Arguments (*args)

Sometimes you don’t know how many arguments will be passed.

Python provides *args for this purpose.

Example:


def total(*numbers):
    print(sum(numbers))
total(10, 20, 30)

Output:

60

How *args Works


def show_numbers(*numbers):
    print(numbers)
show_numbers(1, 2, 3, 4)

Output:

(1, 2, 3, 4)

*args stores arguments as a tuple.

Iterating Through *args


def display(*names):
    for name in names:
        print(name)
display("John", "Emma", "David")

Output:

John
Emma
David

5. Keyword Variable-Length Arguments (**kwargs)

**kwargs allows multiple named arguments.

Example:


def student(**details):
    print(details)
student(name="John", age=20)

Output:

{‘name’: ‘John’, ‘age’: 20}

How **kwargs Works


def employee(**data):
    for key, value in data.items():
        print(key, value)
employee(name="Emma", age=25)

Output:

name Emma
age 25

**kwargs stores data as a dictionary.

Combining Different Argument Types

Python allows combining multiple argument types.

Example:


def employee(name, age=18):
    print(name, age)
employee("John")

Output:

John 18

Function with Return Value

Arguments are often used with return statements.

Example:


def multiply(a, b):
    return a * b
result = multiply(5, 4)
print(result)

Output:

20

Real-Life Examples:

1. Student Marks Calculator


def total_marks(math, science, english):
    return math + science + english
total = total_marks(80, 75, 90)
print(total)

Output:

245

Schools commonly use such calculations.

2. Online Shopping Cart


def calculate_price(price, quantity):
    return price * quantity
print(calculate_price(500, 2))

Output:

1000

E-commerce websites use arguments to process product data.

3. Employee Salary


def salary(hours, rate):
    return hours * rate
print(salary(40, 20))

Output:

800

Payroll systems frequently use function arguments.

4. Banking Application


def deposit(balance, amount):
    return balance + amount
print(deposit(1000, 500))

Output:

1500

5. User Registration


def register(name, email):
    print(name, email)
register(
    "John",
    "john@example.com"
)

Output:

John john@example.com

Advantages of Function Arguments

1. Flexibility

Functions can work with different values.

2. Reusability

One function can handle multiple scenarios.

3. Reduced Code Duplication

No need to create separate functions for every value.

4. Better Readability

Keyword arguments make code easier to understand.

5. Improved Maintainability

Changes can be made in one place.

Common Mistakes

1. Missing Required Arguments

Incorrect:


def greet(name):
    print(name)
greet()

Output:

TypeError

Correct:


greet("John")

2. Incorrect Number of Arguments

Incorrect:


def add(a, b):
    print(a + b)
add(10)

Output:

TypeError

3. Wrong Positional Order

Incorrect:


student(20, "John")

Values may be assigned incorrectly.

4. Forgetting * in *args

Incorrect:


def total(args):
    pass

Correct:


def total(*args):
    pass

5. Forgetting ** in **kwargs

Incorrect:


def student(kwargs):
    pass

Correct:


def student(**kwargs):
    pass

Best Practices

1. Use Meaningful Parameter Names


def calculate_salary(hours, rate):

Instead of:


def calculate_salary(a, b):

2. Use Default Arguments Carefully

Provide logical default values.

3. Prefer Keyword Arguments for Clarity


employee(
    age=25,
    name="John"
)

4. Use *args Only When Needed

Avoid unnecessary complexity.

5. Use **kwargs for Flexible Data

Useful for forms and configuration settings.

Conclusion

Function arguments are a fundamental part of Python programming because they allow functions to receive and process data dynamically. By understanding positional arguments, keyword arguments, default arguments, *args, and **kwargs, developers can create flexible and reusable functions that work efficiently in different scenarios.

Related Python Tutorials