6. Functions
6. Functions
Python Function
A function in Python is a way to group a set of related
statements together to perform a particular operation. It
helps break down complex problems into smaller,
manageable pieces of code.
Code Reusability
Functions allow you to write a piece of code once and use it multiple times
Instead of repeating the same code block, you can call a function whenever needed
Example:
def calculate_area(length, width):
return length * width
• The functions which are come along with Python itself are
called a built-in function or predefined function.
example: range(), id(), type(), input(), eval() etc.
Example of Built in Function
Python range()
Function generates the immutable sequence of
numbers starting from the given start integer to the stop
integer.
for i in range(1,10):#range(start, stop, step)
print(i, end=' ')
# Output 1 2 3 4 5 6 7 8 9
User-defined function
Functions which are created by programmer explicitly
according to the requirement are called a user-defined
function.
Creating a Function
print("Addition: ", a)
print("Subtraction: ", b)
print("Multiplication: ", c)
print("Division: ", d)