Function Arguments
Python tutorial · PySpark.in
Function Arguments
Function arguments are the actual values supplied to a function at the time of its execution. They enable a function to operate on different data values without modifying the function definition. Arguments provide flexibility and reusability to function
Need for Function Arguments
- To pass data into functions
- To avoid writing multiple functions for similar tasks
- To make functions dynamic and reusable
TYPES OF FUNCTION ARGUMENTS IN PYTHON
Python supports four main types of function arguments:
- Positional Arguments
- Keyword Arguments
- Default Arguments
- Variable-Length Arguments
POSITIONAL ARGUMENTS
Positional arguments are arguments that are passed to a function according to the position of parameters defined in the function. The order of arguments is significant in this type.
Characteristics
- Order of arguments must match the order of parameters
- Number of arguments must be equal to number of parameters
- Most commonly used argument type
Example
```
def subtract(a, b):
return a - b
result = subtract(10, 5)
print(result)
```
Understanding the Example
- 10 is assigned to parameter a
- 5 is assigned to parameter b
- Result depends on the order of values passed
Advantages
- Simple and easy to use
- Suitable for small functions
Limitations
- Error-prone when many parameters are involved
- Reduces code readabilityKeyword Arguments
Keyword arguments are arguments that are passed to a function using the parameter names, allowing values to be supplied in any order.
Characteristics
- Order of arguments does not matter
- Improves code readability
- Reduces ambiguity
Example
```
def student(name, age):
print(name, age)
student(age=30, name="Rohan")
```
Understanding the Example
- Python matches arguments directly with parameter names
- Order of arguments is ignored
Advantages
- Clear and self-documenting
- Ideal for functions with many parameters
Limitations
- Slightly longer syntax
- Parameter names must be remembered
DEFAULT ARGUMENTS
A default argument is a parameter that assumes a predefined value if no corresponding argument is provided during function call. Default values are assigned at the time of function definition.
Characteristics
- Makes arguments optional
- Default value is used only when argument is not passed
Example
```
def greet(name="Student"):
print("Hello", name)
greet()
greet("Megha")
```
Understanding the Example
- If no argument is passed, default value is used
- If argument is provided, default value is overridden
Advantages
- Simplifies function calls
- Reduces number of arguments
Limitations
- Care required while using mutable default values
VARIABLE-LENGTH ARGUMENTS
Variable-length arguments allow a function to accept any number of arguments, making the function more flexible.
Types of Variable-Length Arguments
- Non-keyword variable-length arguments (*args)
- Keyword variable-length arguments (**kwargs)
1. *args (Non-keyword Variable-Length Arguments)
*args allows multiple arguments to be passed to a function, which are internally stored as a tuple.
Example
```
def total(*numbers):
sum = 0
for i in numbers:
sum += i
return sum
print(total(1, 2, 3, 4))
```
2. **kwargs (Keyword Variable-Length Arguments)
**kwargs allows a function to accept any number of keyword arguments, which are stored as a dictionary.
Example
```
def details(**data):
for key, value in data.items():
print(key, value)
details(name="Rohan", age=20, course="Msc")
```
Advantages
- Highly flexible
- Useful for library and API development
Limitations
- Can reduce code clarity if overused
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges