Have you ever asked someone to calculate the area of a rectangle?
You might say:
“Calculate the area of a rectangle with length 10 cm and width 5 cm.”
The person needs the length and width to do the calculation.
Python functions work in a similar way.
A function can receive information from the program that calls it. This information is called an argument.
In this lesson, we will learn:
What are function arguments?
Parameters vs arguments
Positional arguments
Keyword arguments
Default arguments
Multiple arguments
Returning results
Common mistakes
Practice challenges
๐ฏ What You'll Learn
By the end of this lesson, you will be able to:
Pass information to a Python function
Understand parameters and arguments
Use positional arguments
Use keyword arguments
Use default arguments
Create functions with multiple arguments
Write useful real-world functions
๐ข Level: Beginner
๐ Suitable for: Classes 7–10
⏱️ Learning time: 25–30 minutes
1. What is a Function?
A function is a reusable block of code designed to perform a particular task.
For example:
def greet():
print("Hello, Student!")
greet()
Output
Hello, Student!
Here:
def greet():
creates a function called greet.
And:
greet()
calls the function.
But what if we want the function to greet different students?
We need to give information to the function.
That's where arguments come in.
2. What is a Function Argument?
An argument is a value that we pass to a function when we call it.
Look at this example:
def greet(name):
print("Hello", name)
greet("Raman")
Output
Hello Raman
Here:
name
is called a parameter.
And:
"Raman"
is called an argument.
Remember
Parameter = variable written in the function definition
Argument = actual value supplied when calling the function
Think of it like this:
Function Definition Function Call
def greet(name): greet("Raman")
↑ ↑
Parameter Argument
3. Why Do We Need Arguments?
Without arguments, we would need to create separate functions for different values.
For example:
def square_of_5():
print(5 * 5)
def square_of_10():
print(10 * 10)
This is unnecessary.
Instead, we can create one reusable function:
def square(number):
print(number * number)
square(5)
square(10)
square(20)
Output
25
100
400
One function can now work with many different values.
That's the real power of function arguments.
4. Positional Arguments
The simplest type of argument is a positional argument.
The values are matched with parameters according to their position.
Example:
def student_info(name, age):
print("Name:", name)
print("Age:", age)
student_info("Raman", 14)
Output
Name: Raman
Age: 14
Here:
name → "Raman"
age → 14
The first argument goes to the first parameter, and the second argument goes to the second parameter.
5. Order Matters
Consider:
def introduce(name, city):
print(name, "lives in", city)
introduce("Raman", "Noida")
Output:
Raman lives in Noida
Now change the order:
introduce("Noida", "Raman")
Output:
Noida lives in Raman
Python doesn't know that we accidentally changed the order.
It simply follows the position.
Therefore:
With positional arguments, order is important.
6. Multiple Arguments
A function can accept several arguments.
For example, let's calculate the total marks of three subjects.
def total_marks(math, science, english):
total = math + science + english
print("Total Marks:", total)
total_marks(85, 90, 88)
Output
Total Marks: 263
Here the function receives three arguments:
math → 85
science → 90
english → 88
We can call the same function again:
total_marks(75, 82, 91)
Output:
Total Marks: 248
7. Keyword Arguments
Python also allows us to specify the parameter name while passing an argument.
These are called keyword arguments.
Example:
def student_info(name, age, city):
print("Name:", name)
print("Age:", age)
print("City:", city)
student_info(
name="Raman",
age=14,
city="Noida"
)
Output
Name: Raman
Age: 14
City: Noida
The advantage is that the meaning of each value is clear.
8. Keyword Arguments Don't Have to Follow the Same Order
With keyword arguments, we can change the order.
def student_info(name, age, city):
print(name, age, city)
student_info(city="Noida", name="Raman", age=14)
Output
Raman 14 Noida
Python matches the values using the parameter names.
So:
city="Noida"
name="Raman"
age=14
is perfectly valid.
9. Positional vs Keyword Arguments
Let's compare them.
Positional
student_info("Raman", 14, "Noida")
The order matters.
Keyword
student_info(
name="Raman",
age=14,
city="Noida"
)
The parameter names are specified, so the order can be changed.
Quick Comparison
| Type | Example | Order Important? |
|---|---|---|
| Positional | fun(10, 20) | Yes |
| Keyword | fun(a=10, b=20) | No |
10. Default Arguments
Sometimes we want a function to have a default value.
For example:
def greet(name="Student"):
print("Hello", name)
greet()
greet("Raman")
Output
Hello Student
Hello Raman
When we don't provide an argument, Python uses the default value.
When we provide an argument, Python uses the supplied value.
11. Real-Life Example – Greeting Students
Suppose we create a function for greeting students.
def greet(name, message="Welcome to Python!"):
print("Hello", name)
print(message)
greet("Raman")
Output
Hello Raman
Welcome to Python!
We can also provide our own message:
greet("Raman", "Good morning!")
Output:
Hello Raman
Good morning!
The default value is used only when we don't provide the second argument.
12. Function Arguments Can Be Different Data Types
Arguments don't always have to be numbers.
They can be:
Strings
Integers
Floats
Lists
Tuples
Dictionaries
Boolean values
Objects
Example:
def show_student(name, marks):
print("Student:", name)
print("Marks:", marks)
show_student("Raman", [85, 90, 88])
Output
Student: Raman
Marks: [85, 90, 88]
13. Returning a Result
A function can receive arguments and return a result.
For example:
def add(a, b):
return a + b
result = add(10, 20)
print("Result:", result)
Output
Result: 30
Here:
add(10, 20)
passes 10 and 20 to the function.
The function calculates:
10 + 20
and returns:
30
14. A Useful School Example – Calculate Percentage
Let's create a function to calculate a student's percentage.
def percentage(math, science, english):
total = math + science + english
percent = total / 300 * 100
return percent
result = percentage(85, 90, 80)
print("Percentage:", result)
Output
Percentage: 85.0
This function can be reused for every student.
print(percentage(85, 90, 80))
print(percentage(70, 75, 80))
print(percentage(95, 92, 96))
Output
85.0
75.0
94.33333333333333
One function can solve the problem for many students.
15. Mixing Positional and Keyword Arguments
We can also combine positional and keyword arguments.
Example:
def student(name, age, city):
print(name, age, city)
student("Raman", age=14, city="Noida")
This is valid.
However, there is an important rule:
Positional arguments must come before keyword arguments.
Correct:
student("Raman", age=14, city="Noida")
Incorrect:
student(name="Raman", 14, city="Noida")
The second example causes a syntax error.
16. What Happens If We Give the Wrong Number of Arguments?
Suppose we have:
def add(a, b):
return a + b
Now we call:
add(10)
Python will report an error because the function requires two arguments.
Similarly:
add(10, 20, 30)
also causes an error because we provided three arguments.
Remember
If a function requires:
def add(a, b):
we normally need to provide exactly two arguments unless default values or special argument mechanisms are being used.
๐ง Think About It
What will this program print?
def multiply(a, b):
return a * b
x = multiply(5, 4)
print(x)
A. 9
B. 20
C. 54
D. Error
Answer: B — 20
Because:
5 × 4 = 20
๐ Debug This Code
Can you find the problem?
def greet(name, age):
print("Hello", name)
print("Age:", age)
greet("Raman")
What's wrong?
The function requires two arguments:
name
age
But only one argument was provided.
Correct code:
greet("Raman", 14)
๐ฏ Mini Project – Student Information
Let's combine everything we have learned.
def student_info(name, age, grade, city="Noida"):
print("----- Student Information -----")
print("Name:", name)
print("Age:", age)
print("Grade:", grade)
print("City:", city)
student_info("Raman", 14, 9)
Output
----- Student Information -----
Name: Raman
Age: 14
Grade: 9
City: Noida
Notice that we didn't provide the city.
Python automatically used:
city="Noida"
because it was the default argument.
⭐ Challenge 1 – Easy
Create a function:
square(number)
that returns the square of a number.
Expected:
print(square(7))
Output:
49
⭐ Challenge 2 – Medium
Create a function:
average(a, b, c)
that returns the average of three numbers.
Example:
print(average(80, 90, 70))
Expected output:
80.0
⭐ Challenge 3 – School Project
Create a function:
student_result(name, math, science, english)
The function should:
Calculate total marks.
Calculate percentage.
Display the student's name.
Display total marks.
Display percentage.
Example:
student_result("Raman", 85, 90, 88)
๐ง Python Pebble Challenge
Can you predict the output?
def calculate(a, b=10):
return a + b
print(calculate(5))
print(calculate(5, 20))
Think before looking at the answer.
Answer
15
25
Why?
First call:
calculate(5)
uses the default value:
5 + 10 = 15
Second call:
calculate(5, 20)
replaces the default value:
5 + 20 = 25
๐ Quick Revision
| Concept | Example |
|---|---|
| Parameter | def add(a, b): |
| Argument | add(10, 20) |
| Positional argument | add(10, 20) |
| Keyword argument | add(a=10, b=20) |
| Default argument | def greet(name="Student") |
| Multiple arguments | add(a, b, c) |
| Return value | return a + b |
๐ Key Takeaways
Remember these five points:
Parameters are variables defined inside the function definition.
Arguments are actual values passed to the function.
Positional arguments depend on order.
Keyword arguments use parameter names.
Default arguments provide a value when the caller doesn't supply one.
The biggest advantage of arguments is reusability.
Instead of writing:
square_of_5()
square_of_10()
square_of_20()
we can write one function:
def square(number):
return number * number
and use it for any number:
square(5)
square(10)
square(20)
That's the power of functions in Python! ๐
๐ What's Next?
Now that you understand function arguments, the next topics to explore are:
Function Return Values → Local & Global Variables → Variable-Length Arguments (*args) → Keyword Variable-Length Arguments (**kwargs) → Lambda Functions
Keep coding, keep experimenting, and remember:
Small code. Big ideas. One Python Pebble at a time! ๐ชจ๐
No comments:
Post a Comment