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); } } 
5
  • Can you pin point like what is it that you don't understand? Commented Oct 3, 2014 at 5:41
  • the output is this .. 0 1 2 .. . i want to understand this output and also what happens when i==0 and the return statement is executed ? Commented Oct 3, 2014 at 5:47
  • this is ridicules that you are asking result for that stackoverflow is for whom they try . or be specific about the question Commented Oct 3, 2014 at 5:50
  • i am not asking the result .. i can get the o/p ..all i want was the concept behind it Commented Oct 3, 2014 at 5:56
  • Recursion: noun, see "Recursion". Commented Oct 3, 2014 at 6:28

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

so what happens is after i==0 condition comes , the stack is emptied and we get the o/p ?
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
it is basically to do not get that particular situtation
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.