I am studying tail recursion and I am not sure I am getting the definition right.
I saw this post's best answer (link) and am wondering if the method sum(int x, int num) is considered tail-recursive in the following example:
public class tailrecusiontest {
private static int total;
private static int add(int num)
{
total+=num;
return total;
}
public static int sum(int x, int num) //THIS METHOD TAIL-RECURSIVE?
{
if(num == 0)
{
return x;
}
return sum(add(num), num-1);
}
public static void main(String[] args) {
System.out.print("" + sum(0,4));
}
}
I.e: What is stopping me to create an object and have call a method ( add(int num) in my example) that will handle the data, give back an output and, by definition, call it the method tail-recursive?
I am asking this question because an assignment is asking me if I can make a method "tail-recursive", and wonder how far the definition of tail-recursion extends.
This means that I can create any method and pass in the values then call it tail-recursive