Function Arguments — Complete Note
Functions can receive different types of arguments to make them flexible, reusable, and more powerful. The main types are positional, default, arbitrary, keyword, and arbitrary keyword arguments.
1. Positional Arguments
These arguments must be passed in the correct order.
The function assigns them based on their position.
Example:
def add(a, b):
return a + b
add(3, 5) # 3 → a, 5 → b
2. Default Arguments
Default arguments have predefined values.
If you don’t pass a value, the default is used.
Example:
def greet(name="User"):
print("Hello", name)
greet() # Uses "User"
greet("Alice") # Overrides default
*3. Arbitrary Arguments (Var-Length Positional Arguments) — args
Used when you don’t know how many positional arguments will be passed.
Example:
def total(*numbers):
return sum(numbers)
total(1, 2, 3, 4)
4. Keyword Arguments
Arguments are passed using name=value format.
Order does not matter.
Example:
def info(name, age):
print(name, age)
info(age=25, name="John")
**5. Arbitrary Keyword Arguments — kwargs
Used when you don’t know how many named (keyword) arguments will be passed.
Example:
def settings(**options):
print(options)
settings(volume=10, mode="dark", brightness=80)