Ask your own question, for FREE!
Mathematics 10 Online
OpenStudy (anonymous):

I called: example3(2); public static void example3(int n){ if(n>0){ example3(n-1); } System.out.print(n + " "); } I know the output is 0 1 2. I don't know why the 2 is outputted though. First thing that happens is example3(2-1), which is 1. Then example3(1-1), which is 0. But where does the 2 come from?

OpenStudy (anonymous):

You have the following function defined: public static void example3(int n) { if (n > 0) example3(n - 1); System.out.print(n + " "); } If you call example3(2), this is what happens: 1. example3(1) is called. 2. example3(0) is called. 3. 0 is displayed and we return to step 2. 4. 1 is displayed and we return to step 1. 5. 2 is displayed and the program terminates.

OpenStudy (anonymous):

Remember that this is not a tail-recursive function, and as a result, things still happen even after the base case is reached.

OpenStudy (anonymous):

Thank you!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!