Tutorials Home   >   Programming Basics   >   Keyword Arguments

Keyword Arguments

Keyword Arguments in Functions

1. Introduction to Keyword Arguments

In programming, functions can accept parameters (inputs). Normally, arguments are passed to a function based on their position, which is called positional arguments.

However, sometimes remembering the order of parameters can be confusing, especially if a function has many parameters. To solve this, most programming languages (like Python) allow keyword arguments.

Keyword arguments allow you to pass values to parameters by specifying their names, rather than relying on their position.


2. What Are Keyword Arguments?

Definition:

A keyword argument is an argument passed to a function by explicitly naming the corresponding parameter.

This makes the function call more readable and flexible.

Real-Life Example:

  • Filling a form online:
    Instead of filling in fields in order, you can specify:
    name="Amit", age=16, grade="10th"


3. Positional Arguments vs Keyword Arguments

Positional Arguments Keyword Arguments
Values assigned based on position Values assigned based on parameter name
Must follow function order Can be in any order
Less readable for many parameters More readable and clear
Example: func(5, 10) Example: func(x=5, y=10)

4. Syntax of Keyword Arguments (Python Example)

def greet(name, message):
print(message + ", " + name)
# Using keyword arguments
greet(message=“Hello”, name=“Amit”)

Output:

Hello, Amit

Explanation:

  • name and message are explicitly assigned

  • The order does not matter


5. Keyword Arguments with Default Parameters

Keyword arguments are often used with default parameters to make function calls more flexible.

def greet(name="Student", message="Welcome"):
print(message + ", " + name)
greet() # Uses all defaults
greet(name=“Riya”) # Overrides name
greet(message=“Hello”) # Overrides message
greet(name=“Riya”, message=“Hi”) # Overrides both

Output:

Welcome, Student
Welcome, Riya
Hello, Student
Hi, Riya

6. Advantages of Keyword Arguments

  1. Improves readability – You know which value is assigned to which parameter

  2. Order flexibility – Parameters can be given in any order

  3. Optional parameters – Can override only the values needed

  4. Reduces errors – Less chance of mixing up argument order


7. Combining Positional and Keyword Arguments

  • Rule: Positional arguments must come before keyword arguments.

Example:

def info(name, age, grade):
print(name, age, grade)
info(“Amit”, grade=“10th”, age=16) # ✅ Works
info(name=“Riya”, 17, “9th”) # ❌ Error

Explanation:

  • All positional arguments first, then keyword arguments


8. Arbitrary Keyword Arguments (**kwargs)

In Python, you can create functions that accept any number of keyword arguments using **kwargs.

Example:

def student_info(**kwargs):
for key, value in kwargs.items():
print(key, ":", value)
student_info(name=“Amit”, age=16, grade=“10th”)

Output:

name : Amit
age : 16
grade : 10th

Explanation:

  • **kwargs collects all extra keyword arguments as a dictionary

  • Useful for flexible functions


9. Keyword Arguments with Return Values

Keyword arguments work well with functions that return values.

def calculate(a=0, b=0, operation="add"):
if operation == "add":
return a + b
elif operation == "subtract":
return a - b
print(calculate(a=5, b=3)) # 8
print(calculate(b=10, a=20, operation=“subtract”)) # 10


10. Common Uses of Keyword Arguments

  • Improving code readability

  • Overriding default parameters

  • Creating functions that accept many optional parameters

  • Designing flexible APIs

  • Handling user inputs in any order


11. Common Mistakes by Beginners

  1. Mixing the order of positional and keyword arguments incorrectly

  2. Using keyword names that do not exist in the function

  3. Forgetting that positional arguments must come first

  4. Overriding default parameters accidentally

  5. Confusing *args (positional) with **kwargs (keyword)


12. Best Practices for Keyword Arguments

  • Use keyword arguments for functions with many parameters

  • Always put positional arguments before keyword arguments

  • Prefer descriptive names for clarity

  • Use **kwargs only when needed for flexible input

  • Comment keyword arguments if their meaning is not obvious


13. Keyword Arguments vs Default Parameters

Feature Keyword Arguments Default Parameters
Passed by naming parameter Defined in function signature
Can override any parameter Used if argument not provided
Flexible order Follow order if overridden positionally
Optional Optional if default exists

14. Conclusion

Keyword arguments are a powerful feature in functions that improve readability, flexibility, and reduce errors. They allow:

  • Passing values without worrying about order

  • Overriding default parameters easily

  • Writing clean, understandable, and reusable code