Back to Python Notes
Topic #222

Function Arguments

By the end of this lesson, you will be able to pass data into Python functions using positional and keyword arguments, understanding how order and naming affect function calls.

What it is

Function arguments are values passed to a function when it is called. These values become local variables inside the function, allowing the code to operate on specific data provided by the caller. Python supports two primary ways to pass these values: positional arguments, where the value’s meaning depends on its position in the call, and keyword arguments, where the value is assigned to a parameter by name.

The mental model is simple: think of parameters as labeled boxes defined in the function signature. Positional arguments drop items into boxes based on left-to-right order. Keyword arguments explicitly label which box receives which item, ignoring order constraints for those specific inputs.

Why it matters

  • Reusability: Functions can perform the same logic on different data without rewriting code.
  • Clarity: Keyword arguments make complex calls readable by explicitly stating what each value represents.
  • Flexibility: Mixing positional and keyword arguments allows developers to override defaults or specify optional parameters easily.
  • Error Prevention: Using keyword arguments reduces bugs caused by swapping similar-looking values (e.g., two integers).

Syntax or steps

When defining a function, list parameters separated by commas. When calling it, provide arguments matching those parameters.

  1. Define the function with named parameters: def func_name(param1, param2):
  2. Call using positional syntax: func_name(value1, value2)
  3. Call using keyword syntax: func_name(param1=value1, param2=value2)

Example

def add(a, b):
    return a + b

# Positional arguments
result1 = add(2, 3)

# Keyword arguments
result2 = add(a=2, b=3)

# Mixed (positional must come before keyword)
result3 = add(2, b=3)

print(result1, result2, result3)

In this example, add(2, 3) assigns 2 to a and 3 to b because they appear in that order. add(a=2, b=3) achieves the same result but explicitly names the parameters. The mixed call add(2, b=3) works because the first argument is positional (assigned to a) and the second is keyword (assigned to b). All three produce the output 5 5 5.

Common mistakes

  • Misordering positional args: Calling subtract(10, 5) vs subtract(5, 10) yields different results if the function relies on order. Use keyword arguments for clarity when parameters have similar types.
  • Keyword after positional: You cannot write add(b=3, 2). All positional arguments must precede keyword arguments in a function call.
  • Missing required arguments: Calling add(2) raises a TypeError because b has no default value and was not provided.
  • Unknown keywords: Calling add(x=2, y=3) fails if the parameters are named a and b. Ensure keyword names match parameter definitions exactly.

When to use it

Choose between positional and keyword styles based on readability and API design.

Style Best For Risk
Positional Simple functions with few, obvious parameters (e.g., max(1, 2)). Harder to read if many similar types are passed.
Keyword Complex functions, optional parameters, or self-documenting code. More verbose; requires exact parameter name knowledge.

Practice

Guided Exercise: Write a function greet(name, greeting="Hello"). Call it once with only the name, and once with both name and greeting using keyword syntax.

Challenge: Create a function calculate_area(length, width). Call it with length=5, width=10. Then try calling it with width=10, length=5. Does the result change? Why?

Solution Hint: The result should remain 50 in both cases because keyword arguments bind values to names, not positions.

Quick check

Question: What happens if you call my_func(key=value, positional_arg)?

Answer: A SyntaxError occurs because positional arguments must always come before keyword arguments in a function call.

Summary

Python function arguments allow flexible data passing through positional ordering or explicit keyword naming. Understanding the rules—specifically that positional arguments precede keyword arguments—is essential for writing robust, readable code that avoids common type errors.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Function Arguments – FAQs

Quick answers about learning Function Arguments in Python.

This free note from Coding Now Tech Institute explains Function Arguments in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Function Arguments, is 100% free with no signup required.
With focused practice, most students grasp Function Arguments in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now