Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Primer for Understanding Recursion

Similar presentations


Presentation on theme: "A Primer for Understanding Recursion"— Presentation transcript:

1 A Primer for Understanding Recursion
The Function Stack A Primer for Understanding Recursion

2 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!

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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...


Download ppt "A Primer for Understanding Recursion"

Similar presentations


Ads by Google