Functions in Python are defined using the def keyword and are used to encapsulate a block of code that performs a specific task. Functions allow for code reusability and better organization.
def my_function(param1, param2):
# Function block
return param1 + param2
# Calling the function
result = my_function(5, 3)-
Parameters and Arguments: Parameters are variables in a function definition. When a function is called, the values passed to it are called arguments.
-
Default Arguments: Functions can have default values for parameters.
def greet(name, message="Hello"): return f"{message}, {name}" print(greet("Alice")) # Outputs: Hello, Alice
-
Keyword Arguments: Allows you to pass arguments in any order by explicitly specifying the parameter names.
def display_info(name, age): print(f"Name: {name}, Age: {age}") display_info(age=30, name="Bob") # Outputs: Name: Bob, Age: 30
-
Variable-length Arguments:
*argsfor variable number of positional arguments and**kwargsfor variable number of keyword arguments.def func(*args, **kwargs): print(args) # Tuple of positional arguments print(kwargs) # Dictionary of keyword arguments func(1, 2, three=3, four=4)
-
Return Statement: Used to exit a function and return a value.
Recursion in Python is a common technique where a function calls itself. It's used to solve problems that can be broken down into smaller, repeatable tasks.
def factorial(n):
if n == 1: # Base case
return 1
else:
return n * factorial(n - 1) # Recursive call
print(factorial(5)) # Outputs: 120-
Base Case: The condition under which the recursion ends. Without a base case, the function would call itself indefinitely, leading to a stack overflow.
-
Recursive Call: The function calling itself with a modified parameter.
-
Stack Frames: Each recursive call adds a layer to the stack, which is why recursive solutions can be memory-intensive.
-
Use Cases: Commonly used in sorting algorithms (like quicksort and mergesort), tree and graph traversal, and dynamic programming.
- Recursive functions can be less efficient than iterative solutions due to the overhead of multiple function calls and stack usage.
- Python has a limit on the depth of recursive calls (default is 1000). Deep recursion might lead to a
RecursionError. - Recursion can be more intuitive and easier to implement for certain problems, like tree traversals.
Functions and recursion are fundamental concepts in Python, enhancing code reusability, readability, and maintainability. While recursion provides a clean and elegant way to solve certain problems, it's essential to be aware of its limitations and alternatives like iterative solutions.