What Are Functions in Python?
A function is a reusable block of code designed to perform a specific task.
Instead of writing the same code repeatedly, you can define a function once and call it whenever required.
Functions help make programs more organized, readable, and easier to maintain.
For example, if you need to add two numbers multiple times, you can create a function for addition.
def add():
a = 10
b = 20
print(a + b)
add()Output:
30
Here, def is the keyword used to define a function, and add() is the function call.
1. How to Define a Function in Python
Python uses the def keyword to create a function.
Syntax:
def function_name():
statements
Example:
def welcome():
print("Welcome to Python")
welcome()Output:
Welcome to Python
In this example:
- def defines the function.
- welcome is the function name.
- The indented statement is the function body.
- welcome() calls the function.
2. Function Parameters and Arguments
Parameters and arguments allow you to pass information to a function.
Example:
def greet(name):
print("Hello", name)
greet("Rahul")Output:
Hello Rahul
Here, name is the parameter, and "Rahul" is the argument passed during the function call.
You can also use multiple parameters.
def add(a, b):
print(a + b)
add(10, 20)Output:
30
3. Return Statement in Python
The return statement is used to send a result from a function back to the caller.
Example:
def add(a, b):
return a + b
result = add(10, 20)
print(result)Output:
30
The returned value can be stored in a variable and used in other calculations.
4. Types of Function Arguments
Python supports different ways of passing arguments to functions.
Positional Arguments
Values are passed according to the position of parameters.
def student(name, age):
print(name, age)
student("Amit", 22)Keyword Arguments
Values are passed using parameter names.
def student(name, age):
print(name, age)
student(age=22, name="Amit")Default Arguments
A default value is used when an argument is not provided.
def greet(name="Guest"):
print("Hello", name)
greet()
greet("Rahul")Output:
Hello Guest
Hello Rahul
Variable-Length Arguments
Python allows functions to accept a variable number of arguments.
The *args syntax is used for multiple positional arguments.
def add(*numbers):
print(sum(numbers))
add(10, 20, 30)Output:
60
The **kwargs syntax accepts multiple keyword arguments.
def details(**data):
print(data)
details(name="Amit", age=25)5. Types of Functions in Python
Python functions can be broadly categorized into two types.
Built-in Functions
These functions are already provided by Python.
Examples include:
- print()
- input()
- len()
- type()
- sum()
- max()
- min()
Example:
numbers = [10, 20, 30] print(len(numbers)) print(sum(numbers))
User-Defined Functions
These are functions created by programmers using the def keyword.
Example:
def square(number):
return number * number
print(square(5))Output:
25
6. Lambda Functions in Python
A lambda function is a small anonymous function that can containa single expression.
It is created using the lambda keyword.
Example:
square = lambda x: x * x print(square(5))
Output:
25
Lambda functions are useful for short operations, particularly when working with functions such as map() and filter().
7. Recursive Functions
A recursive function is a function that calls itself.
Example:
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5))Output:
120
In this example, the function calls itself with smaller values until it reaches the stopping condition.
Advantages of Functions in Python
Functions provide several benefits:
- Code reusability.
- Better program organization.
- Reduced code repetition.
- Easier debugging and testing.
- Improved readability.
- Easier program maintenance.
Conclusion
Functions are an essential part of Python programming. They allow developers to divide large programs into smaller, reusable blocks of code.
Understanding function definitions, parameters, arguments, return statements, lambda functions, and recursion will help you write clean and efficient Python programs.
Practice creating different functions and using them in small projects to strengthen your programming skills.
