recursion function code example

Example 1: what is recursion

Recursion is a basic programming 
technique you can use in Java,
in which a method calls itself to
solve some problem. A method that 
uses this technique is recursive.
Many programming problems can be
solved only by recursion, and some
problems that can be solved by other 
techniques are better solved by recursion.

EXP-1: Factorial
	
	public static long factorial(int n){
        if (n == 1)
            return 1;
        else
            return n * factorial(n-1);
    }

EXP-2: Fibonacci
	
	static int n1=1, n2=2, n3=0;
    public static void printFibo(int count){
        if (count > 0) {
            n3 = n1 + n2;
            n1 = n2;
            n2 = n3;
            System.out.print(" " + n3);
            printFibo(count-1);
        }
    }

Example 2: recursive function javascript

function pow(x, n) {
  if (n == 1) {
    return x;
  } else {
    return x * pow(x, n - 1);
  }
}

alert( pow(3, 3) ); // 27

Example 3: recursion

# modified code tranclated to python from (Drab Duck)

def fact(num):
  if num <= 1:
      return 1
  else:
    return num*fact(num-1)

Example 4: recursion

Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion
Did you mean: recursion

Example 5: recursion

Click the Did you mean

Example 6: Recursion

/**
 * Return all subsequences of word (as defined above) separated by commas,
 * with partialSubsequence prepended to each one.
 */
private static String subsequencesAfter(String partialSubsequence, String word) {
    if (word.isEmpty()) {
        // base case
        return partialSubsequence;
    } else {
        // recursive step
        return subsequencesAfter(partialSubsequence, word.substring(1))
             + ","
             + subsequencesAfter(partialSubsequence + word.charAt(0), word.substring(1));
    }
}

Tags:

Misc Example