A function is a block of code designed to perform a specific task.
Functions help organize code
into reusable modules, improving readability and maintainability. In programming, functions can
be classified into various types based on parameter passing and return type.
Classification of User-Defined Functions
User-defined functions can be categorized into four types based on whether they accept
parameters and return a value:
---
1. Functions with No Parameters and No Return Value
These functions do not take any input parameters and do not return a value.
Example: Used for simple tasks like printing a message.
def greet():
print("Hello, welcome to the program!")
# Calling the function
greet()
---
2. Functions with Parameters but No Return Value
These functions accept parameters (inputs) but do not return a value.
Example: Used when operations are performed using provided inputs.
def display_message(name):
print(f"Hello, {name}! Welcome!")
# Calling the function
display_message("John")
---
3. Functions with No Parameters but a Return Value
These functions do not take parameters but return a value to the caller.
Example: Used when a fixed computation or value is required.
def get_pi():
return 3.14159
# Calling the function
pi_value = get_pi()
print("Value of Pi:", pi_value)
---
4. Functions with Parameters and Return Value
These functions accept parameters and return a value after performing operations on the input.
Example: Used for tasks requiring both input and output.
def add_numbers(a, b):
return a + b
# Calling the function
result = add_numbers(5, 7)
print("Sum:", result)
---