Recursion


 * Recursion** **-** is when a function calls itself over and over again.

Here are some simple steps on how to create a Recursion method. 1.) Create a base case 2.) Call the Method within itself until the base case is met.


 * __WARNING!__**: Make sure you have a base case so that it is possible for the program to end. Otherwise, the code will never stop. THIS COULD CAUSE PHYSICAL DAMAGE TO YOUR COMPUTER IF THE CODE RUNS TOO LONG! If the code doesn't stop because you forgot a base case, use CTRL-ALT-DEL and force quit BlueJ/Greenfoot.

Here is an example of improper recursion due to no base case:

code format="java" public int example(int number) {  return(integer - (example(number - 1));   //There is no base case in this code, so it will loop indefinitely } code

Another example, this time with an improper base case (assume the variable number = 10):

code format="java" public int example(int number) {   if(integer == 1) {       return 1; }   else {       return(integer - (example(integer - 2));    } /*This time we have a base case, but it only is activated if integer equals  exactly 1. If we go through the code, we will get this:   10 - 8 - 4 - 2 - 0... Integer never equals 1, so the code will never stop */ } code

Now here's an example on what a recursion method looks like and how to identify what exactly is going on. code int myFactorial( int integer) {   if( integer == 1) {       return 1; }   else {       return(integer*(myFactorial(integer-1));    } } code --integer == 1 is the base case, this makes it so the method will end at some point. --If the base case's condition isn't met it goes on to the recursion part of the program. Now let's say integer = 3 1.) Check if the base case condition is met since Integer = 3 and not 1 it skips the base case. 2.) since Integer = 3 the code does (3 * ( repeating the method)) 3.) since it says integer - 1 ( 3 - 1), integer now becomes 2 4.) It repeats the method once more, integer = 2 now and not 1 so it skips the base case. 5.) since Integer = 2 now the code does ( 3 * ( 2 * ( repeating the method))) 6.) since it says integer - 1 ( 2 - 1), integer now becomes 1 7.) now that Integer = 1 the base case becomes true and the Final output would become 3 * 2 * 1 = 6 8.) done!
 * Base Case Portion**:
 * Recursion Portion:**

Hope this example somewhat helped you understand what Recursion is about and how to identify it

QUIZ: 1.)Recursion does not need a base case. a.) True b.) False

2.) Recursion is not dangerous and should be used whenever a programmer feels like it. a.) True b.) False

3.) What would be returned if integer is 1? code format="java5" int bam( int integer) if (integer == 1)   {        return 1;    }

else return(integer - (bam(integer - 1)));

code

4.) What would be returned if integer is 4? code format="java" int bam( int integer) if (integer == 1)   {        return 1;    } else    return(integer - (bam(integer - 1))); code

answers: 1.) b 2.) b 3.) 1 4.) (4 - ( 3 - ( 2 - ( 1)))) which would become 2