Functions in Python
A function in Python is a reusable block of code designed to perform a specific task. Functions help divide large programs into smaller parts, improve readability, and reduce repetition.
Functions in Python can be classified in two main ways:
- Based on Definition
- Based on Structure
1. Classification Based on Definition
I. Built-in Functions
Built-in functions are predefined in Python. You do not need to create them.
Example:
print("Hello")
len([1, 2, 3])
type(10)
int("5")Explanation:
print("Hello")displays output on the screen.len([1, 2, 3])returns the number of elements in the list, which is 3.type(10)tells you the data type of the value, which isint.int("5")converts the string"5"into the integer5.
These functions are ready-made tools provided by Python.
II. User-Defined Functions
User-defined functions are created by the programmer using the def keyword.
Example:
def greet(name):
print("Hello", name)
greet("Mohan")Explanation:
def greet(name):defines a function namedgreetwith one parametername.print("Hello", name)prints a greeting message.greet("Mohan")calls the function and passes"Mohan"as input.
Output:
Hello MohanHere, the function runs only when it is called.
III. Lambda (Anonymous) Functions
Lambda functions are small, unnamed functions created using the lambda keyword.
Example:
square = lambda x: x * x
print(square(5))Explanation:
lambda x: x * xcreates a small function that returns the square ofx.- It is assigned to the variable
square. square(5)returns25.
Lambda functions are used for short, simple operations.
2. Classification Based on Structure
Functions can also be classified based on whether they accept parameters and whether they return values.
There are four structural types.
I. Function Without Parameters and Without Return Value
Example:
def welcome():
print("Welcome to Python")
welcome()Explanation:
- The function takes no input.
- It does not return any value.
- It simply prints a message.
- Every time it is called, it produces the same output.
II. Function With Parameters but Without Return Value
Example:
def greet(name):
print("Hello", name)
greet("Mohan")Explanation:
- The function accepts input (
name). - It does not return a value.
- It prints a message based on the input provided.
The output changes depending on the argument passed.
III. Function Without Parameters but With Return Value
Example:
def get_pi():
return 3.14
value = get_pi()
print(value)Explanation:
- The function does not take input.
- It returns a fixed value (
3.14). - That returned value is stored in the variable
value. - Then it is printed.
IV. Function With Parameters and With Return Value
Example:
def add(a, b):
return a + b
result = add(5, 3)
print(result)Explanation:
- The function accepts two inputs (
aandb). - It calculates their sum.
- It returns the result.
- The returned value is stored in
result.
Output:
8This is the most flexible and commonly used function structure.
Conclusion
Functions in Python are classified based on how they are defined (built-in, user-defined, lambda) and based on their structure (parameters and return values). Understanding both classifications helps in writing clean and organized programs.
