🧠 Mastering Python Functions

Table of contents
- Everything You Need to Know (Beginner to Pro)
- 📌 So, What Exactly is a Function?
- ⚙️ Parameters vs Arguments
- 🧪 Types of Arguments
- ✨ Meet *args and **kwargs
- 🧠 Behind the Scenes: How Functions Live in Memory
- ❌ What If a Function Doesn’t Return Anything?
- 🌍 Local vs Global Scope
- 🔁 Functions Inside Functions (a.k.a. Nested)
- 🎯 Functions Are First-Class Citizens
- ✅ Wrapping Up

Everything You Need to Know (Beginner to Pro)
📌 So, What Exactly is a Function?
In Python, a function is just a reusable block of code that takes some input, does some work, and gives you an output.
Here’s a simple one:
def is_even(num):
"""
This function returns whether a number is odd or even.
input - any valid integer
output - odd/even
created on - 16th Nov 2022
"""
if type(num) == int:
if num % 2 == 0:
return 'even'
else:
return 'odd'
else:
return 'pagal hai kya?'
Calling the Function:
for i in range(1, 11):
print(is_even(i))
Want to Know What the Function Does?
Use the docstring!
print(is_even.__doc__)
⚙️ Parameters vs Arguments
A parameter is what you write in the function definition.
An argument is what you pass in the function call.
def greet(name): # 'name' is the parameter
print(f"Hello, {name}!")
greet("Zainab") # "Zainab" is the argument
🧪 Types of Arguments
Let’s break down how you can pass values to a function:
✅ Default Arguments
def power(a=1, b=1):
return a ** b
print(power()) # 1
✅ Positional Arguments
Position matters…
print(power(2, 3)) # 8
✅ Keyword Arguments
Called by key.
print(power(b=3, a=2)) # 8 (same result, but more readable!)
✨ Meet *args
and **kwargs
They make your functions super flexible!
*args
= Multiple positional arguments
when you don’t want to bound your function with limited number of inputs.
Now you can use function for many inputs
def multiply(*args):
product = 1
for i in args:
product *= i
return product
multiply(2, 3, 4) # 24
**kwargs
= Multiple keyword arguments
def display(**kwargs):
for key, value in kwargs.items():
print(f"{key} -> {value}")
display(india='delhi', srilanka='colombo')
🧠 Behind the Scenes: How Functions Live in Memory
When your script starts, Python creates a global frame.
Every time you call a function, it gets its own memory frame.
Once the function finishes, that frame is destroyed — neat and clean.
❌ What If a Function Doesn’t Return Anything?
It still works... but it returns None
.
def is_even(num):
if num % 2 == 0:
print("even")
else:
print("odd")
x = is_even(3)
print(x) # Output: None
Use return
if you want to do something with the result.
🌍 Local vs Global Scope
x = 5
def f():
x = 1
x += 1
print(x) # 2
f()
print(x) # 5
Want to change the outer variable from inside the function? Use global
.
x = 5
def f():
global x
x += 1
f()
print(x) # 6
🔁 Functions Inside Functions (a.k.a. Nested)
Yes, you can define a function inside another:
def outer():
def inner():
print("Inside inner")
inner()
print("Inside outer")
🎯 Functions Are First-Class Citizens
In Python, functions are treated just like variables.
1. You can assign them:
f = lambda x: x * x
2. You can pass them around:
def func_b(z):
return z()
func_b(lambda: print("Called"))
3. You can return them:
def f():
def x(a, b):
return a + b
return x
print(f()(3, 4)) # 7
✅ Wrapping Up
Functions are the heart of Python programming. They help you:
Avoid repetition
Keep your code clean
Organize logic like a pro
Once you master functions, you're no longer a beginner — you're leveling up. 🚀
Subscribe to my newsletter
Read articles from Bibi Zainab directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
