A Primer for Understanding Recursion The Function Stack A Primer for Understanding Recursion
Let’s trace this code. Always start with main! class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Let’s trace this code. Always start with main!
Main is pushed onto the activation stack and has 3 things to do class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Main is pushed onto the activation stack and has 3 things to do 1 method2(); 2 method1(); 3 PRINT(…); main
Time to call method2() main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Time to call method2() 1 method2(); 2 method1(); 3 PRINT(…); main
method2() is pushed onto the stack class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } method2 method2() is pushed onto the stack 1 method2(); 2 method1(); 3 PRINT(…); main
method2() has 1 thing to do class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } method2 1 PRINT(…); method2() has 1 thing to do 1 method2(); 2 method1(); 3 PRINT(…); main
method2 main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Method 2 was called method2 1 PRINT(…); 1 method2(); 2 method1(); 3 PRINT(…); main
method2 finishes and dies class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } method2 finishes and dies Method 2 was called method2 1 PRINT(…); 1 method2(); 2 method1(); 3 PRINT(…); main
Return to main for next item main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Return to main for next item Method 2 was called 1 method2(); 2 method1(); 3 PRINT(…); main
Call method1 and put on the stack class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Call method1 and put on the stack 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called 1 method2(); 2 method1(); 3 PRINT(…); main
method1 main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called 1 method2(); 2 method1(); 3 PRINT(…); main
method1 calls method2 method1 main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } method1 calls method2 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called 1 method2(); 2 method1(); 3 PRINT(…); main
Put method2 on the stack method2 method1 main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Put method2 on the stack method2 1 PRINT(…); 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called 1 method2(); 2 method1(); 3 PRINT(…); main
method2 method1 main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } method2 1 PRINT(…); 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called 1 method2(); 2 method1(); 3 PRINT(…); main
method2 dies and is removed class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } method2 dies and is removed method2 1 PRINT(…); 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called 1 method2(); 2 method1(); 3 PRINT(…); main
Return to method1 method1 main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Return to method1 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called 1 method2(); 2 method1(); 3 PRINT(…); main
method1 main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called Method 1 still alive 1 method2(); 2 method1(); 3 PRINT(…); main
method1 removed from stack class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } method1 removed from stack 1 PRINT(…); 2 method2(); 3 PRINT(…); method1 Method 2 was called Method 1 was called Method 1 still alive 1 method2(); 2 method1(); 3 PRINT(…); main
Go to third thing in main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Go to third thing in main Method 2 was called Method 1 was called Method 1 still alive 1 method2(); 2 method1(); 3 PRINT(…); main
main class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Method 2 was called Method 1 was called Method 1 still alive Finished! 1 method2(); 2 method1(); 3 PRINT(…); main
Remove main from the stack class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } Remove main from the stack Method 2 was called Method 1 was called Method 1 still alive Finished! 1 method2(); 2 method1(); 3 PRINT(…); main
End of program class Main { static void method2() { PRINT("Method 2 was called"); } static void method1() { PRINT("Method 1 was called"); method2(); PRINT("Method 1 still alive"); public static void M/main (S/string[] args){ method2(); method1(); PRINT("Finished!"); } End of program Method 2 was called Method 1 was called Method 1 still alive Finished! Follow the white rabbit...