Module: M3-R5: Python Programming
Chapter: Functions
Keyword arguments allow you to pass values to a function using the parameter names. This makes your function calls more readable and allows arguments to be passed in any order.
Syntax:
def function_name(param1, param2):
# code block
function_name(param1=value1, param2=value2)
Example:
def greet(name, age):
print(f"Hello {name}, you are {age} years old")
# Using keyword arguments
greet(age=24, name="Sourav")
Output:
Hello Sourav, you are 24 years old
Positional arguments must come first, followed by keyword arguments.
def order(item, quantity=1):
print(f"Order: {quantity} {item}")
# Valid calls
order("Pizza") # Uses default quantity
order("Burger", quantity=2) # Overrides default
Output:
Order: 1 Pizza
Order: 2 Burger