The Function Stack A Primer for Understanding Recursion

  • Slides: 22
Download presentation
The Function Stack A Primer for Understanding Recursion

The Function Stack A Primer for Understanding Recursion

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Let’s trace this code. Always start with main!

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Main is pushed onto the activation stack and has 3 things to do main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Time to call method 2() main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } method 2() is pushed onto the stack method 2 main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } method 2() has 1 thing to do method 2 main 1 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called method 2 main 1 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called method 2 finishes and dies method 2 main 1 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Return to main for next item Method 2 was called main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Call method 1 and put on the stack method 1 main 1 PRINT(…); 2 method 2(); 3 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called method 1 main 1 PRINT(…); 2 method 2(); 3 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called method 1 calls method 2 method 1 main 1 PRINT(…); 2 method 2(); 3 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Put method 2 on the stack method 2 1 PRINT(…); method 1 1 PRINT(…); 2 method 2(); 3 PRINT(…); main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called method 2 1 PRINT(…); method 1 1 PRINT(…); 2 method 2(); 3 PRINT(…); main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called method 2 dies and is removed method 2 1 PRINT(…); method 1 1 PRINT(…); 2 method 2(); 3 PRINT(…); main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called Return to method 1 main 1 PRINT(…); 2 method 2(); 3 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called Method 1 still alive method 1 main 1 PRINT(…); 2 method 2(); 3 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called Method 1 still alive method 1 removed from stack method 1 main 1 PRINT(…); 2 method 2(); 3 PRINT(…); 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called Method 1 still alive Go to third thing in main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called Method 1 still alive Finished! main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called Method 1 still alive Finished! Remove main from the stack main 1 method 2(); 2 method 1(); 3 PRINT(…);

class Main { static void method 2() { PRINT("Method 2 was called"); } static

class Main { static void method 2() { PRINT("Method 2 was called"); } static void method 1() { PRINT("Method 1 was called"); method 2(); PRINT("Method 1 still alive"); } public static void M/main (S/string[] args){ method 2(); method 1(); PRINT("Finished!"); } } Method 2 was called Method 1 was called Method 2 was called Method 1 still alive Finished! Follow the white rabbit. . . End of program