Introduction: Functions are blocks of code that perform a specific task. They allow you to break down your program into smaller, reusable parts. This promotes code reusability and enhances the readability and maintainability of your code.
Advantages of Functions:
- Code Reusability: Functions allow you to reuse the same block of code multiple times without rewriting it.
Types of Functions in Python:
- Built-in Functions: These are functions that are provided by Python, such as
id(),print(),type(), etc. - User-defined Functions: These are functions defined by the user to fulfill specific requirements.
Syntax of a Function:
def function_name(parameters):
# Function body
# Perform operations using parameters
return value
defis mandatory and indicates the start of a function definition.returnstatement is optional and is used to return a value from the function.
Example:
def greet():
print("Hello, World!")
greet()
Parameters and Arguments:
- Parameters: These are variables listed inside the parentheses in the function definition.
- Arguments: These are the values passed to the function when it is called.
Example:
def greet(name):
print("Hello,", name)
greet("Alice")
Return Statement: Functions can optionally return a value using the return statement. This value can then be used wherever the function call is made.
Example:
def add(a, b):
return a + b
result = add(3, 5)
print(result) # Output: 8
Types of Arguments:
- Positional Arguments: These are arguments passed to a function in the correct positional order.
- Keyword Arguments: These are identified by parameter names, so the order of passing them is not important.
- Default Arguments: These are arguments that take a default value if no value is provided in the function call.
- Variable-Length Arguments: These allow you to pass a variable number of arguments to a function
Examples for Different Types of Arguments:
- Positional Arguments:
- These are arguments passed to a function in the correct positional order.
def greet(name, message):
print(message, name)
# Calling the function with positional arguments
greet("Alice", "Hello") # Output: Hello Alice
- Keyword Arguments:
- These are identified by parameter names, so the order of passing them is not important.
def greet(name, message):
print(message, name)
# Calling the function with keyword arguments (order doesn't matter)
greet(message="Hi", name="Bob") # Output: Hi Bob
- Default Arguments:
- These are arguments that take a default value if no value is provided in the function call.
def greet(name, message="Hello"):
print(message, name)
# Calling the function without providing the 'message' argument
greet("Charlie") # Output: Hello Charlie
- Variable-Length Arguments:
- These allow you to pass a variable number of arguments to a function.
def sum_values(*args):
total = 0
for num in args:
total += num
return total
# Calling the function with variable-length arguments
print(sum_values(1, 2, 3)) # Output: 6
print(sum_values(1, 2, 3, 4, 5)) # Output: 15
These examples demonstrate how different types of arguments can be used in Python functions, offering flexibility and versatility in function calls.