
Functions in Python
A function is a reusable block of code that performs a particular task. We write the code once inside a function and can call it whenever we need it.
let's understand Python functions in a simple way, including args , **args ,**kwargs , default parameters, scope, closures, and lambda function.
What is a Function in Python?
A function is created using the def keyword.
def greet():
print("Hello Sathwik!")
greet()
Output
Hello Sathwik!
Here, greet() is the function. When we call greet(), Python executes the code inside it.
Function with Parameters
Sometimes we want to send some information to a function. We can do this using parameters.
def greet(name):
print("Hello", name)
greet("Sathwik")
Output
Hello Sathwik
Here, name is a parameter, and “Sathwik” is the value passed to that parameter.
Function with Return Value
A function can also return a result using the return keyword.
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output
30
The return statement sends the result back to the place where the function was called.
Default Parameters
A default parameter is a parameter that already has a value.
def greet(name="Sathwik"):
print("Hello", name)
greet()
Output
Hello Sathwik
If we provide another value, the default value is replaced.
def greet(name="Sathwik"):
print("Hello", name)
greet("Rahul")
Output
Hello Rahul
Default parameters are useful when we want a function to work even if the user does not provide a particular value.
What is agrs in Python?
args means argument , the value passed to that parameter
def greet(name):
print("Hello", name)
greet("Sathwik")
Output
Hello Sathwik
Here, name is a parameter, and “Sathwik” is argument , the value passed to that parameter
What is *agrs in Python?
Sometimes we don't know how many arguments will be passed to a function.
This is where *args is useful.
*args allows a function to accept multiple positional arguments.
def add_numbers(*args):
total = 0
for number in args:
total += number
return total
print(add_numbers(10, 20))
print(add_numbers(10, 20, 30, 40))
Output
30
100
*args → multiple positional arguments
What is **kwargs in Python?
**kwargs is used when we want to pass multiple keyword arguments to a function.
Keyword arguments are passed using a parameter name and value.
def student_details(**kwargs):
for key, value in kwargs.items():
print(key, ":", value)
student_details(
name="Sathwik",
age=21,
course="Python"
)
Output
name : Sathwik
age : 21
course : Python
Inside the function, kwargs behaves like a dictionary.
**kwargs → multiple keyword arguments
Scope in Python
Scope means the area where a variable can be accessed.
There are mainly two important scopes when learning functions:
- Local scope
- Global scope
Local Scope
A variable created inside a function is called a local variable.
def student():
name = "Sathwik"
print(name)
student()
Here, name is available inside the function.
We cannot normally access it outside the function.
def student():
name = "Sathwik"
student()
# print(name) # This will cause an error
Global Scope
A variable created outside a function has global scope.
name = "Sathwik"
def greet():
print(name)
greet()
Output
Sathwik
The function can access the global variable because it is defined outside the function.
Closures in Python
A closure happens when an inner function remembers and uses a variable from its outer function even after the outer function has finished executing.
def welcome(name):
def message():
print("Welcome", name)
return message
greet = welcome("Sathwik")
greet()
Output
Welcome Sathwik
Here, message() is an inner function.
It uses the name variable from the outer function welcome().
Even after welcome() has finished, the inner function remembers the value “Sathwik”.
This is called a closure.
Closures are useful when we want a function to remember some information.
Lambda Functions
A lambda function is a small anonymous function.
Instead of using def, we can create a simple function using the lambda keyword.
For example:
square = lambda x: x * x
print(square(5))
Output
25
The same thing using a normal function would be:
def square(x):
return x * x
print(square(5))
Lambda functions are generally useful when we need a small function for a short operation.
Join Techsnap Creators
Share your knowledge and earn ??
Want to showcase your tech expertise and get rewarded for your insights? Join the Techsnap creator network!
Write insightful blogs, stay ahead of industry trends, and grow your professional brand while helping others in the community.
Ready to make an impact?

Comments