1. What is a Function in Python?
A function is a block of reusable code that performs a specific task. Instead of writing the same code repeatedly, we can define a function once and call it whenever needed.
Functions help to reduce code repetition, improve code readability, and make programs easier to maintain.
Python provides the
defkeyword to define a function.
Syntax:
def function_name():
# function body
statements
Example:
def greet():
print("Hello, Welcome to Python")
greet()
Output:
Hello, Welcome to Python
Explanation:
-
defis the keyword used to define a function. -
greetis the function name. - The statements inside the function form the function body.
-
greet()is the function call used to execute the function.
2. Why Do We Use Functions?
Functions provide several advantages:
- Code Reusability – Write code once and use it multiple times.
- Code Readability – Makes the program easier to understand.
- Code Organization – Divides a large program into smaller parts.
- Easy Maintenance – Changes can be made in one place.
- Avoid Code Repetition – Reduces duplicate code.
3. Types of Functions in Python
Python functions can be classified into two main types:
- Built-in Functions
- User-defined Functions
1. Built-in Functions
Built-in functions are predefined functions provided by Python. We can use them directly without defining them.
Some commonly used built-in functions are:
| Function | Description |
|---|---|
| print() | Displays output |
| input() | Gets input from the user |
| len() | Returns the length |
| type() | Returns the data type |
Example:
numbers = [10, 20, 30, 40]
print(len(numbers))
Output:
4
2. User-defined Functions
User-defined functions are functions created by programmers to perform specific tasks.
The def keyword is used to create user-defined functions.
Example:
def add():
a = 10
b = 20
print(a + b)
add()
Output:
30
4. Types of User-defined Functions Based on Arguments and Return Values
Based on arguments and return values, functions can be divided into four types.
1. Function Without Arguments and Without Return Value
This function does not accept any arguments and does not return any value.
Example:
def display():
print("Welcome to Python")
display()
Output:
Welcome to Python
2. Function With Arguments and Without Return Value
This function accepts arguments but does not return any value.
Example:
def add(a, b):
print(a + b)
add(10, 20)
Output:
30
Here, a and b receive the values passed during the function call.
3. Function Without Arguments and With Return Value
This function does not accept arguments but returns a value using the return keyword.
Example:
def add():
a = 10
b = 20
return a + b
result = add()
print(result)
Output:
30
Here, the function calculates the result and sends it back to the caller using return.
4. Function With Arguments and With Return Value
This function accepts arguments and returns a result.
Example:
def add(a, b):
return a + b
result = add(10, 20)
print(result)
Output:
30
This is one of the most commonly used function types in Python.
Conclusion
Functions are an important concept in Python programming. They help developers write reusable, organized, and maintainable code.
Understanding function definitions, arguments, parameters, return statements provides a strong foundation for developing Python applications.
Top comments (0)