FUNCTIONS AND
RECURSION
IN PYTHON
WHAT IS A FUNCTION?
A function is a block of reusable code that
performs a specific task.
Helps in code reusability, organization,
and readability.
Defined using the def keyword.
FUNCTION PARAMETERS
AND ARGUMENTS
Parameters: Variables inside function definition.
Arguments: Values passed when calling the function.
WHY USE F-STRINGS?
Readability – f-strings make it easy to embed variables
inside a string.
Conciseness – It avoids using concatenation (+) or .format().
Performance – f-strings are faster than .format() and
concatenation.
Alternative Approaches
Instead of f'Hello, {name}!', you could write:
RETURN STATEMENT
Functions can return values using the return keyword.
RECURSION
Recursion is when a function calls itself to solve a smaller
instance of a problem.
Each recursive call moves closer to a base case to prevent
infinite loops.
EXAMPLE – FACTORIAL USING
RECURSION
Factorial formula: n! = n × (n-1)
× ... × 1
UNDERSTANDING RECURSIVE CALLS
How factorial(3) works:
1. factorial(3) → 3 * factorial(2)
2. factorial(2) → 2 * factorial(1)
3. factorial(1) → 1 (Base case)
POWER FUNCTION
Implement a recursive function to calculate
x^n.
THANK YOU!
07/03/2025
TWO-DIMENSIONAL LISTS
14/03/2025
MIDTERM QUIZ