recursion - Java recursive method difference -
what difference between following 2 methods:
public boolean recursionmethodone(node n) { system.out.println(n.getvalue()); return recursionmethodone(n.next()); } public void recursionmethodtwo(node n) { system.out.println(n.getvalue()); recursionmethodtwo(n.next()); }
which 1 use recursion , difference?
thanks
both codes doesn't exits. need add return test condition. example:
public void recursionmethodtwo(node n) { if (n == null) { // standard way exit void function without executing remaing code // note return null; doesn't compile return; } system.out.println(n.getvalue()); recursionmethodtwo(n.next()); }
returning value or not depends on kind of function. example if need calculate factorial need result, if need print list don't.
so example seems method 2 closer needs.
otherwise need ask returning boolean value of function? if have nice answer question can implement code returning value.
Comments
Post a Comment