Page 361 - computer science (868) class 11
P. 361
Or we can define it in a different way as:
It should be like this
F(1) = a
F(2) = a x F(1)
F(3) = a x F(2)
....
F(n) = a x F(n-1)
We find that in each step we are calling the same function with a different argument, it is called the recursive case.
We stop calling the function when we reach 'n'.
So, we conclude that a recursive function is made up of three components which are as follows:
Base case Recursive case
Base case is a condition in recursion In recursive case the function calls on
to stop it. Otherwise the function will itself directly or indirectly, with changed
call itself infinitely until the program value of argument.
crashes.
Dry run/working of the preceding program is as follows:
Counter value Condition Executable Statements Output Counter next value
3 3 = 0 false mymethod(3) Hello 3 Step 3 popping 2
System.out.println(" "+2) mymethod(3)
2 2 = 0 false mymethod(2) Hello 2 Step 2 popping 1
System.out.println(" "+1) mymethod(2)
1 1 = 0 false mymethod(1) Hello 1 Step 1 popping 0
System.out.println(" "+0) mymethod(1)
0 0 = 0 true Base case reached. Blank print
Now stack is popped
counter Counter = 0 Push in stack Output pushing Popping Output popping
Program 1 The following function witty() is a part of some class. What will be the output of the
function witty() when the value of n is “SCIENCE” and the value of p is 6. Show the dry run/
working: [ISC 2012]
1 class Trial1
2 {
3 public void witty(String n, int p)
4 {
5 if(p<0)
6 System.out.println(" ");
7 else
359
Recursion 359

