Function Calling
Function Calling
-
Basic Function Calls:
- The example demonstrates how functions can call each other. When
first()
is executed, it callssecond()
, which in turn callsthird()
. - The output order is
third
,second
,first
, illustrating the call stack: the last function called completes first, and control returns to the previous function.
- The example demonstrates how functions can call each other. When
-
Traffic-Signal Method:
- Functions can be in three states: ongoing, suspended, or completed.
- Ongoing: When control is inside the function and executing code.
- Completed: When all lines have been executed, and control has exited the function.
- Suspended: When a function calls another function and awaits its completion.
- Functions can be in three states: ongoing, suspended, or completed.
-
Stack Representation:
- The stack visualizes the state of function calls at different times, showing how control flows between functions as they complete.
Recursion
-
Recursive Functions:
- A function that calls itself, such as the factorial function
fact()
. - It includes a base case to terminate recursion; without it, recursion can lead to infinite calls.
- A function that calls itself, such as the factorial function
-
Example of Factorial Calculation:
- The function halts execution temporarily to execute
fact(n - 1)
until reachingfact(0)
, which returns1
, allowing the stack to unwind.
- The function halts execution temporarily to execute
-
Fibonacci Series:
- Demonstrates another recursive function,
fibo()
, to compute Fibonacci numbers. - The naive recursive implementation can lead to excessive computation and inefficiency due to repeated calculations of the same values (e.g.,
fibo(2)
is calculated multiple times).
- Demonstrates another recursive function,
-
Performance Measurement:
- The time taken for a function to run can be measured using the
time
library. - The naive recursive solution for Fibonacci is inefficient, especially for large inputs.
- The time taken for a function to run can be measured using the
-
Iterative Solution:
- An iterative approach to the Fibonacci series avoids the inefficiency of recursion by maintaining state with variables.
Counting Function Calls
- A global variable can be used to count how many times a function has been called, demonstrated with the factorial example.
Infinite Recursion and Errors
- Pathological Recursion:
- A function without a base case can lead to a
RecursionError
due to exceeding the maximum recursion depth (typically 1000 in Python). - Checking the recursion limit can be done using
sys.getrecursionlimit()
.
- A function without a base case can lead to a
Summary
- This content serves as an introduction to understanding function calls and recursion in Python. It explains key concepts such as the call stack, function states, recursive versus iterative approaches, performance issues, and potential pitfalls like infinite recursion.