Functions are the heart of any Python program. They help organize code, make it reusable, and keep it neat. But to truly master functions, you need to understand how parameters and arguments work — and all the different types Python supports.
Let’s dive into Python functions and explore the different types of parameters with real, easy-to-understand examples!
🧠 What is a Function in Python?
A function is a block of organized and reusable code that performs a specific task.
You define a function with the def
keyword, and you call it like this:
⚙️ Built-in vs User-defined Functions
Type | Example |
---|---|
Built-in | len() , print() , max() |
User-defined | Functions you create with def |
📦 Components of a Function
add
: function namea
,b
: parametersreturn
: sends result back when function is called
🧩 Types of Parameters in Python
Python supports several ways to pass information to functions. Understanding these helps you build flexible and powerful programs.
1️⃣ Positional Parameters
These are the most basic — values are passed in order.
Here, x
gets 3 and y
gets 4 — based on position.
2️⃣ Default Parameters
You can set a default value for a parameter.
If no value is passed, Python uses the default.
3️⃣ Keyword Arguments
Here, you specify which parameter gets which value — order doesn’t matter.
4️⃣ Variable-length Arguments (*args
)
Use *args
to pass any number of positional arguments.
Here, marks
becomes a tuple of values.
5️⃣ Variable-length Keyword Arguments (**kwargs
)
Use **kwargs
to pass any number of keyword arguments.
Output:
Here, details
becomes a dictionary.
🚧 Common Mistakes to Avoid
❌ Mixing positional and keyword arguments in the wrong order:
❌ Forgetting the base case in recursion or missing return in functions.
🧪 Quick Practice Questions
- Write a function to calculate the average of any number of scores using
*args
. - Create a function that takes a student's name and grade with the default grade as 10.
- Write a function to display student details using
**kwargs
.
✅ Conclusion
Understanding Python’s different parameter types gives you more power and flexibility when writing functions. From simple positional arguments to advanced *args
and **kwargs
, mastering these makes you a stronger, more confident Python programmer.
So the next time you're writing a function, don’t just stop at def fun(a, b)
— explore the world of arguments Python offers! 🐍💡