Tutorials Home   >   Core Programming Concepts   >   Variable Length Arguments

Variable Length Arguments

1. Introduction to Variable-Length Arguments

In programming, functions often have a fixed number of parameters. But what if you don’t know in advance how many inputs a function will receive?

To handle such situations, programming languages like Python provide variable-length arguments. These allow a function to accept any number of arguments, making the function flexible and reusable.


2. What Are Variable-Length Arguments?

Definition:

Variable-length arguments are function parameters that allow a function to accept an arbitrary number of inputs.

This is useful when:

  • The exact number of inputs is unknown

  • Inputs can vary each time the function is called

  • You want to write a flexible, reusable function

Real-Life Example:

  • A shopping cart function that sums prices of any number of items

  • A survey function that counts responses from any number of participants


3. Types of Variable-Length Arguments in Python

Python supports two types of variable-length arguments:

  1. Arbitrary Positional Arguments (*args)

  2. Arbitrary Keyword Arguments (**kwargs)


4. Arbitrary Positional Arguments (*args)

*args allows a function to accept any number of positional arguments.
The arguments are collected into a tuple.

Syntax:

def function_name(*args):
# args is a tuple containing all positional arguments

Example:

def add_numbers(*numbers):
total = 0
for num in numbers:
total += num
return total
print(add_numbers(1, 2, 3)) # 6
print(add_numbers(5, 10, 15, 20)) # 50

Explanation:

  • numbers collects all arguments in a tuple

  • The function works for any number of inputs


5. Arbitrary Keyword Arguments (**kwargs)

**kwargs allows a function to accept any number of keyword arguments.
The arguments are collected into a dictionary, with the key as the parameter name and the value as its value.

Syntax:

def function_name(**kwargs):
# kwargs is a dictionary

Example:

def student_info(**info):
for key, value in info.items():
print(key, ":", value)
student_info(name=“Amit”, age=16, grade=“10th”)

Output:

name : Amit
age : 16
grade : 10th

Explanation:

  • kwargs collects all named arguments in a dictionary

  • Useful when you don’t know the names or number of inputs


6. Combining Positional, *args, Default, and **kwargs

Functions can combine all types of arguments in one definition. The order matters:

  1. Standard positional arguments

  2. Default arguments

  3. *args (arbitrary positional)

  4. **kwargs (arbitrary keyword)

Example:

def demo(a, b=5, *args, **kwargs):
print("a =", a)
print("b =", b)
print("args =", args)
print("kwargs =", kwargs)
demo(1, 2, 3, 4, 5, x=10, y=20)

Output:

a = 1
b = 2
args = (3, 4, 5)
kwargs = {'x': 10, 'y': 20}

Explanation:

  • a and b are standard/default arguments

  • *args collects extra positional arguments

  • **kwargs collects extra keyword arguments


7. Advantages of Variable-Length Arguments

  1. Functions can accept any number of inputs

  2. Code becomes flexible and reusable

  3. Reduces redundancy (no need to write multiple functions)

  4. Can handle dynamic input easily


8. Common Uses of Variable-Length Arguments

  • Summing any number of numbers (*args)

  • Collecting user input dynamically

  • Logging variable data using keyword arguments (**kwargs)

  • Passing parameters to APIs where argument names vary

  • Flexible mathematical or utility functions


9. Common Mistakes by Beginners

  1. Placing *args after **kwargs (order must be correct)

  2. Forgetting the * or ** before the parameter name

  3. Confusing *args (tuple) with **kwargs (dictionary)

  4. Modifying args or kwargs unnecessarily

  5. Ignoring standard or default parameters when combining with *args and **kwargs


10. Best Practices for Variable-Length Arguments

  • Use *args for extra positional arguments

  • Use **kwargs for extra named arguments

  • Always follow the correct order when combining argument types

  • Keep function logic clear and readable

  • Document what arguments the function can accept


11. Example: Practical Use Case

def shopping_cart(*items, **discounts):
total = sum(items)
for discount_name, amount in discounts.items():
total -= amount
return total
print(shopping_cart(100, 200, 300, coupon=50, member_discount=30))

Output:

520

Explanation:

  • *items collects all product prices

  • **discounts collects all discounts by name

  • Function calculates total cost dynamically


12. Variable-Length Arguments vs Default Parameters

Feature Variable-Length Arguments Default Parameters
Number of inputs Any number Fixed (but optional)
Flexible ✅ Partially
Collected as Tuple (*args) / Dictionary (**kwargs) Single variable
Useful when Inputs vary When common default exists

13. Conclusion

Variable-length arguments allow functions to handle flexible and unknown numbers of inputs, making them powerful and reusable.

For learners, understanding variable-length arguments is essential for:

  • Writing dynamic and flexible functions

  • Handling optional or multiple inputs efficiently

  • Avoiding writing multiple similar functions for different numbers of arguments