Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Usmanu Danfodiyo University, Sokoto

Department of Computer Science

CMP 202: Computer Programming II

RECURSION

1
CONTENTS

❖ What is Recursion?

❖ Examples

❖ Halting Condition

❖ Advantages & Disadvantages of Recursion

2
WHAT IS RECURSION?

3
Introduction to Recursion
❖ A process in which a function calls itself directly or indirectly is
called recursion and the corresponding function is called a
recursive function.

❖ Using a recursive algorithm, certain problems can be solved


quite easily

❖ This technique provides a way to break complicated problems


down into simple problems which are easier to solve.
4
Example
❖ A Method that prints Hi

public class RecursionMthd{


public static void main (String [] args){
sayHi();
}
private static void sayHi(){
System.out.print (“Hi!”);
sayHi();
}
}
❖ This will print Hi! Over 1000 times and the terminates the program with an error message.
❖ Whenever a java program is running, anytime it enters a new method, it puts all information such as
the name of the method, references to variables etc. for the method into a “call stack”
❖ When it finishes executing the method, it will take the information off the call stack.
❖ But, in this scenario, it will be putting and removing the information on the call stack until when the
stack overflow error occurs and the program stops.
5
Example (Cont)…
❖ We need a exit strategy that will make the program terminate.

public class RecursionMthd{


public static void main (String [] args){
sayHi(100);
}
private static void sayHi(int count){
System.out.print (“Hi!”);
if (count<=1){
return;
}
sayHi(count -1);
}
}
❖ This will print Hi 100 times and then terminates without any error.
❖ Base case is needed whenever you are using recursion in programming, if not used, the method will
call itself infinitely.
❖ A base case simply refers when a program should terminate or stop.
6
Example: Factorial using Recursion
❖ Calculating the Factorial of 5
❖ Factorial of a number is calculated by multiplying the number with all numbers it
preceded.
❖ The Factorial of 5 will be 5*4*3*2*1

public class Factorial{


public static void main (String [] args){
System.out.println(factorial(5));
}
private static int factorial(int n){
if (n==1){
return 1;
}
else {
return n* factorial(n-1);
}
}
❖ OUTPUT: 120 7
Example: Factorial using Recursion (Cont…)
public class Factorial{
public static void main (String [] args){
System.out.println(factorial(5));
}
private static int factorial(int n){
if (n==1){
System.out.println(“factorial(“+ n +”) = 1”);
return 1;
}
else {
System.out.println(“factorial(“+ n +”) = “ + n +” * factorial(“ + (n-1) +”)”);
return n* factorial(n-1);
}
}
}
OUTPUT
Factorial(5) = 5 * factorial(4)
Factorial(4) = 4 * factorial(3)
Factorial(3) = 3 * factorial(2)
Factorial(2) = 2 * factorial(1)
Factorial(1) = 1
120 8
Halting Condition
❖ Just as loops can run into the problem of infinite looping, recursive functions
can run into the problem of infinite recursion.
❖ Infinite recursion is when the function never stops calling itself.
❖ Every recursive function should have a halting condition (Base case), which
is the condition where the function stops calling itself.
❖ The developer should be very careful with recursion as it can be quite easy
to slip into writing a function which never terminates, or one that uses
excess amounts of memory or processor power.
❖ However, when written correctly recursion can be a very efficient and
mathematically-elegant approach to programming.
9
Advantages & Disadvantages of recursion
❖ Advantages

✓ Recursion provides a clean and simple way to write code.

✓ Some problems are inherently recursive like tree traversals, Tower of Hanoi, etc. For such
problems, it is preferred to write recursive code.

❖ Disadvantages

✓ The recursive program has greater space requirements than the iterative program as
all functions will remain in the stack until the base case is reached.

✓ It also has greater time requirements because of function calls and returns overhead.

Note: Both recursive and iterative programs have the same problem-solving powers, i.e.,
every recursive program can be written iteratively and vice versa is also true.
10
SNAP TEST

11
Snap Test

Write a recursive method to get the factorial of 3 and print the output

12
Solution
public class Factorial{
public static void main (String [] args){
System.out.println(factorial(3));
}
private static int factorial(int n){
if (n==1){
System.out.println(“factorial(“+ n +”) = 1”);
return 1;
}
else {
System.out.println(“factorial(“+ n +”) = “ + n +” * factorial(“ + (n-1) +”)”);
return n* factorial(n-1);
}
}
}
OUTPUT
Factorial(3) = 3 * factorial(2)
Factorial(2) = 2 * factorial(1)
Factorial(1) = 1
6
13

You might also like