Functions in Python
A function in Python is a reusable block of code that performs a specific task. Functions help make programs more organized, readable, and reduce repetition.
We can define our own functions using the def keyword. Once defined, the function can
be
called (used) multiple times.
Syntax:
def function_name():
# block of code
statements
# Calling the function
function_name()
Note: Function name should follow naming rules and it should be meaningful. The block of code inside the function is indented.
Types of Functions in Python
In Python, functions are mainly of two types:
- Built-in Functions (Predefined / Inbuilt Functions): These are already provided
by Python.
Examples include
print(),len(),type(),sum()etc. - User-defined Functions: These are the functions we create ourselves using the
defkeyword, to perform a specific task as per our need.
Example of Built-in Function:
numbers = [1, 2, 3, 4]
print("Length is:", len(numbers))
Output:
Length is: 4
Explanation:
len()is a built-in function that returns the number of items in a list.- We didn't have to define
lenourselves. Python provides it by default.
Example of User-defined Function:
Example 1: Basic function without parameters
def greet():
print("Hello, welcome to Python Functions with ShikshaSanchar!")
greet()
Output:
Hello, welcome to Python Functions with ShikshaSanchar!
Explanation:
- We created a function named
greet()using thedefkeyword. - The function prints a welcome message.
- We called the function by writing
greet()and it displayed the message.
Example 2: Function with parameters
def add_numbers(a, b):
result = a + b
print("Sum is:", result)
add_numbers(5, 7)
Output:
Sum is: 12
Explanation:
- The function
add_numbers()takes two parameters:aandb. - Inside the function, it calculates their sum and prints it.
- We call the function with arguments
5and7, so output isSum is: 12.
Click the next page to know more about Arguments and Return values in Python.
Parts of a Function (Table):
| Part | Description |
|---|---|
def keyword |
Used to define a function. |
| Function name | Name of the function (should follow variable naming rules). |
| Parameters | Optional. Values that we can pass to the function. |
| Function body | The block of code that runs when the function is called. |
| Return statement | Optional. Used to return a value from the function. |
Advantages of Functions
- Reusability: Once a function is written, we can use it multiple times without rewriting the code.
- Modularity: Large programs can be divided into small functions which makes code organized and manageable.
- Easy Debugging: Errors can be easily found and fixed because we can test functions independently.
- Improves Readability: Functions make the program easier to understand by breaking it into smaller parts.
- Avoids Repetition: Reduces duplication of code which saves time and effort.
Summary:
- A function is a reusable block of code.
- Functions are defined using the
defkeyword. - Functions can have parameters (input values).
- Functions make our code clean, short, and reusable.