Function Arguments in Python
In Python, when we define a function, we can pass values (arguments) into it. These arguments allow the function to work with dynamic inputs instead of fixed values.
Function Arguments and return statement There are following types of arguments that we can provide in a function:
- Positional Arguments
- Keyword Arguments
- Default Arguments
- Variable length Arguments
- Keyword-Only Arguments (using *)
- Positional-Only Arguments (Python 3.8+ using /)
- Required Arguments
1. Positional Arguments Positional arguments are arguments passed to a function in the correct order (position). Python matches each argument by its position in the function call.
Example: Basic Function with Positional Arguments.
Python
# Define a function with 2 positional arguments
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
# Calling with positional arguments
greet("Ashish", 29) Output Hello Ashish, you are 29 years old.
Explanation:
- name → first parameter → gets "Ashish"
- age → second parameter → gets 29
⚠️ Order Matters! If we change the order of positional arguments, the output changes accordingly:
Python
greet(29, "Ashish")
Output Hello 29, you are Ashish years old.
2. Keyword Arguments When calling a function, if you pass arguments using the parameter names (keywords) — that’s called keyword arguments.
Python matches arguments by name, not by position.
Example: Basic Keyword Arguments.
Python
# Define a simple function
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
# Calling with keyword arguments
greet(age=29, name="Ashish") Output Hello Ashish, you are 29 years old.
Rule When calling a function, we must pass all positional arguments before keyword arguments.
Python
def greet(name, age):
print(name, age)
greet("Ashish", age=29) # ✅ correct
greet(name="Ashish", 29) # ❌ error ❌ Error: SyntaxError: positional argument follows keyword argument
3. Default Arguments Default arguments are parameters that have a default value assigned in the function definition. If we don’t provide a value for that parameter when calling the function, Python uses the default value automatically.
We can provide a default value to an argument by using the assignment operator (=). Any number of default arguments we can have in our function.
Syntax:
def function_name(param1, param2=default_value):
# function body • param2 here has a default value.
• param1 is still required (unless it also has a default value).
Example: Basic Default Argument.
Python
def greet(name, age=18): # age has a default value
print(f"Hello {name}, you are {age} years old.")
# Without passing age
greet("Ashish")
# Passing age explicitly
greet("Ashish", 29) Output Hello Ashish, you are 18 years old.
Hello Ashish, you are 29 years old.
Python’s Rule for Default Arguments Once we assign a default value to a parameter, all the parameters that come after it must also have default values (or be optional).
Otherwise, Python throws a SyntaxError.
❌ Incorrect Example (Rule Violation)
Python
def greet(name="Ashish", age): # ❌ INVALID
print(f"Hello {name}, you are {age} years old.") Python immediately complains:
SyntaxError: non-default argument follows default argument
👉 Why? Because name has a default value, but age (which comes after) does not.
This breaks the rule.
4. Variable-length arguments Sometimes we may need to pass more arguments than those defined in the actual function. This can be done using variable-length arguments.
There are two ways to achieve this:
• *args *args allows a function to accept any number of positional arguments (0 or more).
• args is just a convention; you can name it anything (e.g., *numbers).
• The * tells Python to pack extra positional arguments into a tuple.
Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls with an arbitrary number of arguments.
Syntax
def function_name(*args):
# args is a tuple containing all extra positional arguments Example: Basic *args.
Python
def add_numbers(*numbers):
print(numbers) # prints a tuple
total = sum(numbers)
print("Sum:", total)
# Call with different number of arguments
add_numbers(5, 10, 15)
add_numbers(1, 2)
add_numbers() # no arguments Output: (5, 10, 15)
Sum: 30
(1, 2)
Sum: 3
()
Sum: 0
Explanation:
- All arguments passed are packed into the tuple numbers.
- We can then iterate or perform operations like sum().
• **kwargs **kwargs allows a function to accept any number of keyword arguments (0 or more).
• kwargs is just a convention; you can name it anything (e.g., **data).
• The ** tells Python to pack extra keyword arguments into a dictionary.
Syntax
def function_name(**kwargs):
# kwargs is a dictionary containing all extra keyword arguments Example: Basic **kwargs.
Python
def display_info(**info):
print(info) # prints a dictionary
print("Hello", info["name"])
display_info(name="Ashish", age=29, city="Delhi") Output: {'name': 'Ashish', 'age': 29, 'city': 'Delhi'}
Hello Ashish
Explanation:
- All extra keyword arguments are collected in the dictionary info.
- Keys are parameter names, and values are their respective values.
5. Keyword-Only Arguments (using *) Keyword-only arguments are parameters that must be passed using their name (keyword), not by position.
• You define keyword-only arguments after a single * in the function definition.
• Any parameter after * cannot be passed positionally; it must be passed as a keyword.
Syntax
def function_name(positional1, positional2, *, kwarg1, kwarg2):
# kwarg1 and kwarg2 are keyword-only arguments • * → separates positional arguments from keyword-only arguments.
• Arguments after * cannot be passed by position.
Example: Basic Keyword-Only Arguments.
Python
def display_info(name, *, age, city):
print(f"Name: {name}, Age: {age}, City: {city}")
# Correct usage (using keywords for age and city)
display_info("Ashish", age=29, city="Delhi")
# ❌ Incorrect usage (passing age and city positionally)
display_info("Ashish", 29, "Delhi") # TypeError ✅Output for correct usage:
Name: Ashish, Age: 29, City: Delhi
❌ Output for incorrect usage:
TypeError: display_info() takes 1 positional argument but 3 were given
6. Positional-Only Arguments (Python 3.8+ using /) Positional-only arguments must be passed by position — we cannot use their names when calling the function.
• Introduced in Python 3.8 using the / symbol in the function definition.
• Any parameter before / is positional-only.
Syntax
def function_name(pos1, pos2, /, standard1, standard2):
# pos1 and pos2 → positional-only
# standard1 and standard2 → can be positional or keyword • / → separates positional-only parameters from normal parameters.
Example: Basic Positional-Only Arguments.
Python
def greet(name, /, age):
print(f"Hello {name}, you are {age} years old.")
# Must pass name positionally
greet("Ashish", age=29)
# ❌ Incorrect: passing by keyword
greet(name="Ashish", age=29) # TypeError ✅ Output for correct usage:
Hello Ashish, you are 29 years old.
❌ Output for keyword usage:
TypeError: greet() got some positional-only arguments passed as keyword arguments: 'name'
7. Required Arguments in Python Required arguments are parameters that must be passed when calling a function.
• Python does not provide a default value for them.
• If you omit a required argument, Python raises a TypeError.
Syntax
def function_name(param1, param2):
# Both are required arguments All arguments without default values are required.
Example: Basic Required Arguments.
Python
# Function with required arguments
def greet(name, age): # both are required
print(f"Hello {name}, you are {age} years old.")
# Correct call → both arguments given
greet("Ashish", 29)
# Wrong call → missing 'age'
greet("Ashish") ✅ Correct Output:
Hello Ashish, you are 29 years old.
❌ Error Output:
TypeError: greet() missing 1 required positional argument: 'age'