0

I have been very keen to get some hands on recursion but i seem to misunderstand it

i have read a question in a book and i am a bit confused and i don't want to cramp it , I want some solid explanation with stacks also about calling of the functions also

class rectest
{
int values[];

rectest(int i)
{
    values = new int[i]; 
}

 void printarray(int i)
  {
    if(i==0)
        return ; 
    else printarray(i-1);
    System.out.print(values[i-1]+" ");
  }
 }

public class recursion 
{
  public static void main(String args[])
   {
    rectest ob = new rectest(10);
    int i ; 
    for(i=0 ; i<10 ; i++)
        ob.values[i] = i ;
    ob.printarray(10);
   }
}
amol
  • 87
  • 1
  • 12

2 Answers2

1

RECURSION Means Repetition calling of anything again and again

This is one of the Traditional and important program to learn how Recursion works and ? is Recursion and Why Let me take an example of Calculating Factorial thier pseudocode will be like this

function factorial is:

input: integer n such that n >= 1
output: [n × (n-1) × (n-2) × … × 1]

    1. if n is >= 1, return [ n × factorial(n-1) ]
    2. otherwise, return 1

end factorial

now what happens here is that it always return [n*factorial(n-1)] which call itself over and over again

Now Let us consider Your Context

 public class recursion 
    {
      public static void main(String args[])
       {
        rectest ob = new rectest(10);

//here you are initializing the object and calling its constructor and intializing the array having size of 10

int i ; 
for(i=0 ; i<10 ; i++)
    ob.values[i] = i ;

//Here you are assigning every member variable to a certain values like at position 0 value[0]=0

ob.printarray(10); //here u are printing the values of ten values which you have passed now Important thing occurs here

} } Now Look at the printarray() Method here

void printarray(int i)
  {
    if(i==0)
        return ; 
    else printarray(i-1);
//printarray(i-1) here it calls the method itself so as to print all the values recursively 
    System.out.print(values[i-1]+" ");
  }

thats it if you have more query ask

Abhishek Chaubey
  • 2,960
  • 1
  • 17
  • 24
  • so what happens is after i==0 condition comes , the stack is emptied and we get the o/p ? – amol Oct 03 '14 at 06:45
  • 1
    at i==0 if you look on your printarray(int i) method at if condition it checks that and make to get out of it from else because if at that condition if it goes to else it will printarray(-1) which is illegal and makes javaexcption or error both – Abhishek Chaubey Oct 03 '14 at 06:56
  • it is basically to do not get that particular situtation – Abhishek Chaubey Oct 03 '14 at 06:57
0

If you have to understand about recursion you can look at this stack overflow question. What is recursion and when should I use it?

If you want to have better understanding of implementation of recursion in java here is another stack overflow question.How is recursion implemented in Java

Community
  • 1
  • 1
Johny
  • 2,128
  • 3
  • 20
  • 33