Thursday, May 26, 2016

Recursion

In the Java language the programmer is allowed to call a method within a method, the act of doing so is called recursion. the usefulness of recursion is to repeat methods multiple times without the use of a for loop or in other very specific ways. The benefit is efficiency however it is a more difficult way to program. It is also very useful to use radicals and exponents.
public int pow(int num, int exp){
int answer;
if(exp == 1){
   return num;
}
else{
   answer = num +pow(num, exp - 1);
}
return answer;
}
// this method will return the number to the exp power.
A recursive method can also be used to sort and search like in other tutorials in this blog, it helps it to be more effective but it is considerably more complicated, Trees and Graphs also use these kind of methods.

No comments:

Post a Comment