Function Arguments
Here is a detailed explanation of function arguments in Python with examples:
1. No Argument
- A function with no parameters takes no arguments when called.
Example:
def greet():
print("Hello, World!")
greet()
Output:
Hello, World!
In this example, the greet()
function does not require any arguments.
2. Required Arguments (Positional Arguments)
- These arguments must be passed in the correct order when calling the function.
- Missing any required arguments will result in an error.
Example:
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
greet("Alice", 25) # Correct
greet(25, "Alice") # Incorrect order
Output:
Hello Alice, you are 25 years old.
Hello 25, you are Alice years old.
Error if an argument is missing:
greet("Alice") # Missing 'age' argument
Error:
TypeError: greet() missing 1 required positional argument: 'age'
3. Arbitrary Length Argument (*args
)
- If you are unsure about the number of arguments, use
*args
to accept any number of positional arguments. - The
*args
parameter collects the arguments into a tuple.
Example:
def add_numbers(*args):
total = sum(args) # Sum all values in args
print(f"Sum of numbers: {total}")
add_numbers(1, 2, 3) # 3 arguments
add_numbers(10, 20, 30, 40) # 4 arguments
Output:
Sum of numbers: 6
Sum of numbers: 100
Here, *args
allows you to pass any number of arguments, which are collected as a tuple.
4. Keyword Arguments
- Keyword arguments are passed with the parameter name explicitly specified.
- The order of arguments does not matter.
Example:
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
greet(age=25, name="Alice") # Keyword arguments
Output:
Hello Alice, you are 25 years old.
5. Default Arguments
- Default arguments have a predefined value in the function definition.
- If a value is not provided during the function call, the default value is used.
Example:
def greet(name, age=18): # Default value for 'age'
print(f"Hello {name}, you are {age} years old.")
greet("Alice") # Uses default age (18)
greet("Bob", 25) # Overrides default age
Output:
Hello Alice, you are 18 years old.
Hello Bob, you are 25 years old.
Combination of Argument Types
You can combine all these argument types in a function, but they must follow a specific order:
Order:
- Required arguments (positional)
- Default arguments
- Arbitrary length arguments (
*args
) - Keyword arguments
- Arbitrary keyword arguments (
**kwargs
)
Example:
def demo_function(a, b=10, *args, name="Anonymous", **kwargs):
print(f"Positional: a={a}, b={b}")
print(f"*args: {args}")
print(f"Keyword argument: name={name}")
print(f"**kwargs: {kwargs}")
demo_function(1, 20, 30, 40, name="Alice", city="New York", age=25)
Output:
Positional: a=1, b=20
*args: (30, 40)
Keyword argument: name=Alice
**kwargs: {'city': 'New York', 'age': 25}
Summary Table
Argument Type | Syntax | Description | Example |
---|---|---|---|
No Argument | def func(): |
Function without any parameters. | func() |
Required Argument | def func(a, b): |
Arguments that must be provided. | func(1, 2) |
Arbitrary Length Argument | def func(*args): |
Accepts any number of positional arguments. | func(1, 2, 3) → args=(1, 2, 3) |
Keyword Argument | def func(name): |
Arguments passed using parameter names. | func(name="Alice") |
Default Argument | def func(a=10): |
Parameters with default values. | func() → Default, func(5) → Overrides. |
Let me know if you'd like further examples or clarification! 😊
Comments
Post a Comment