java - Is a method that has a method call in one if its parameters called tail recursive? -
i studying tail recursion , not sure getting definition right.
i saw post's best answer (link) , wondering if method sum(int x, int num) considered tail-recursive in 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: stopping me create object , have call method ( add(int num) in example) handle data, give output and, by definition, call method tail-recursive?
i asking question because assignment asking me if can make method "tail-recursive", , wonder how far definition of tail-recursion extends.
this means can create method , pass in values call tail-recursive
your method meets definition of tail recursion, java not have tail recursion optimization. means still have n-sized stack , return through 1 1.
Comments
Post a Comment