jeudi 7 février 2019

Recursion Basics

I have read about recursion and I'm very much interested in understanding a very basic thing which I'm using in the factorial. Since I have researched about it well, saw particular videos of it, but somehow this thing is very much confusing me. I have the option of writing the mug up a code and move ahead, but I'm into learning things well.

From here are the sources for the recursion :

I have one thing to ask since in the code I have seen one thing that is the if-else. I know about if else condition very well. But in here things are a little bit complicated.

public static int fact(int n){
  if(n <=1)
     return 1;
  else 
     return n * fact(n-1);
}

In the above code, here the result appears to be correct but somehow what I think is why it is not returning only 1 when it satisfies the condition, and how come it is printing the correct result. There is a single catch which I'm not been able to grasp. Please help me to get through this.

Consider me as a learner in the coding field. Thanks

Aucun commentaire:

Enregistrer un commentaire