Unit 3 · Functions
Function Arguments
Arguments make functions flexible and powerful. Learn how to pass data into functions using positional arguments, understand the difference between arguments and parameters, and master how Python handles multiple arguments in function calls.
Introduction
A function without arguments is like a calculator with only one fixed operation—it can do only one thing. Arguments allow us to pass different values to the same function so it can work with varying inputs. This is what makes functions truly reusable.
When you call print("Hello"), the string
"Hello" is an argument. When you call
len("Python"), "Python" is the
argument. In both cases, the function receives data through its
arguments and uses it during execution.
This chapter covers positional arguments, passing multiple arguments, argument vs parameter terminology, and how Python assigns values to parameters.
University Definition
Function arguments are the actual values passed to a function when it is called. Parameters are the variable names listed in the function definition. When the function is called, the arguments are assigned to the corresponding parameters.
Parameter vs Argument
Parameter
A parameter is a variable in the function definition.
def greet(name): # 'name' is a parameter
print(name)
Argument
An argument is the actual value passed during the call.
greet("Alice") # "Alice" is an argument
Positional Arguments
The most common type of argument is the positional argument. The value you pass is matched to the parameter based on its position (order).
def greet(first, last):
print("Hello,", first, last)
greet("John", "Doe")
Output: Hello, John Doe
Here, "John" maps to first (position 1)
and "Doe" maps to last (position 2).
If you swap the order, the output changes.
Passing Multiple Arguments
A function can accept any number of arguments, separated by commas:
def add(a, b, c):
total = a + b + c
print("Total =", total)
add(10, 20, 30)
add(5, 10, 15)
Output: Total = 60 Total = 30
The number of arguments in the call must match the number of parameters in the definition (for positional arguments):
def add(a, b, c):
print(a + b + c)
add(10, 20) # TypeError: add() missing 1 argument
How Python Assigns Arguments
Python assigns arguments to parameters in order, from left to right. Each argument is copied into its corresponding parameter variable.
def describe(name, age, city):
print(name, "is", age, "years old and lives in", city)
describe("Alice", 21, "Mumbai")
Output: Alice is 21 years old and lives in Mumbai
Think of it like filling slots: the first argument fills the first parameter, the second fills the second, and so on.
Real-Life Analogy
Imagine a shipping form. The form has fields (parameters): Name, Address, and Phone Number. When you fill in the form (call the function), you provide actual values (arguments) for each field. The shipping company (Python) uses those values in the correct positions to process your order.
If you write the wrong value in the wrong field (wrong order of arguments), the package might go to the wrong address!
Arguments Can Be Any Data Type
Arguments are not limited to strings and numbers. You can pass lists, dictionaries, booleans, and more:
def show_info(name, marks, is_passed):
print("Student:", name)
print("Marks:", marks)
print("Passed:", is_passed)
show_info("Riya", [85, 90, 78], True)
Output: Student: Riya Marks: [85, 90, 78] Passed: True
Passing a List as Argument
def calculate_average(numbers):
total = sum(numbers)
avg = total / len(numbers)
print("Average =", avg)
marks = [85, 90, 78, 92, 88]
calculate_average(marks)
Output: Average = 86.6
The entire list is passed as a single argument. This pattern is very common in real-world programs.
Key Points
Arguments are values passed during the function call.
Parameters are variables in the function definition.
Positional arguments are matched by their order.
The number of arguments must match the number of parameters.
Arguments can be of any data type.
Functions make code reusable by accepting different inputs.
University Exam Tip
Common exam questions on this topic:
- Differentiate between a parameter and an argument.
- What happens if the number of arguments does not match parameters?
- Trace the output of a function call with positional arguments.
- Can a function accept arguments of different data types? Explain.
Common Mistakes
Mismatched Argument Count
def add(a, b):
print(a + b)
add(1, 2, 3) # TypeError: takes 2 positional arguments but 3 were given
Wrong Argument Order
def greet(name, age):
print(name, "is", age, "years old")
greet(21, "Alice") # Runs but output is wrong: 21 is Alice years old
The code runs but the output is logically incorrect. Always match argument order carefully.
Practice Questions
Q1. What is the difference between a parameter and an argument?
Answer: A parameter is a variable in the function definition; an argument is the actual value passed when the function is called.
Q2. Write a function multiply(a, b) that prints the product of two numbers.
def multiply(a, b):
print("Product =", a * b)
multiply(4, 5) # Product = 20
multiply(3, 7) # Product = 21
Q3. What will be the output?
def show(a, b, c):
print(a, b, c)
show(10, 20, 30)
show("A", "B", "C")
Output: 10 20 30 A B C
Q4. Write a function that accepts a list and prints each element.
def print_list(items):
for item in items:
print(item)
print_list([10, 20, 30, 40])
Q5. What error occurs when you call a function with fewer arguments than required?
Answer: A TypeError is raised, stating that the function is missing required positional arguments.
Summary
- Arguments are values passed to functions during a call.
- Parameters are variables in the function definition.
- Positional arguments are matched by their position.
- Argument count must match parameter count for positional arguments.
- Arguments can be any data type including lists, strings, numbers, and booleans.