Function parameters allow you to pass data into a function so it can perform operations using that data.
They make functions flexible and reusable.
WHAT ARE PARAMETERS?
Parameters are variables listed inside the parentheses when defining a function.
Arguments are the actual values passed when calling the function.
def greet(name): # name is a parameter
print("Hello", name)greet("Hira") # "Hira" is an argument
TYPES OF FUNCTION PARAMETERS
Python supports different types of parameters.
1. POSITIONAL PARAMETERS
These are the most common type.
Values are assigned based on their position.
def add(a, b):
return a + bprint(add(5, 3))
Here, 5 is assigned to a and 3 to b.
2. KEYWORD PARAMETERS
You can specify arguments by parameter name.
def student(name, age):
print("Name:", name)
print("Age:", age)student(age=20, name="Ali")
Order does not matter when using keyword arguments.
3. DEFAULT PARAMETERS
You can assign default values to parameters.
def greet(name="Guest"):
print("Hello", name)greet()
greet("Sara")
If no argument is provided, the default value is used.
4. VARIABLE-LENGTH PARAMETERS
Sometimes you donât know how many arguments will be passed.
*args (Non-Keyword Arguments)
def add_numbers(*numbers):
total = 0
for num in numbers:
total += num
return totalprint(add_numbers(1, 2, 3, 4))
*args collects multiple positional arguments into a tuple.
**kwargs (Keyword Arguments)
def display_info(**info):
for key, value in info.items():
print(key, ":", value)display_info(name="Hira", age=25)
**kwargs collects multiple keyword arguments into a dictionary.
IMPORTANT RULES
⢠Positional arguments must come before keyword arguments
⢠Default parameters must come after required parameters
⢠*args comes before **kwargs in function definition
Example order:
def example(a, b=10, *args, **kwargs):
pass
WHY PARAMETERS ARE IMPORTANT
⢠Make functions reusable
⢠Allow dynamic input
⢠Help build flexible applications
⢠Reduce code duplication
Understanding function parameters helps you write powerful and professional Python programs.