+ 8

# I have a question about recursion

I created a method in java called check that uses recursion to calculate something and return boolean. My code didn't work when I just wrote check(); in to method to make recursion, but it did work when I wrote: return check();. Can someone explane me whats the difference? Thank you:)

21st Apr 2018, 7:43 AM
G.G DOMINOES
+ 6
đ
21st Apr 2018, 7:52 PM
G.G DOMINOES
+ 5
Yes but its not where the recursion stops and the method returns a value. It is just where I need the method to call itself. Threre is another place where the method returns true or false after the calculation finished
21st Apr 2018, 7:59 AM
G.G DOMINOES
+ 5
I think you mean about the return symm(a); and the if(symm(a1)). in this case, try to understand the if : if(1234) means nothing. but if(true) or if(false) return a condition.
21st Apr 2018, 8:16 AM
NoxFly
+ 5
The method should check if the array is symmetric. For example the array {2, 4, 7, 8, 11, 13} is symmetric because 2+13 = 15, 4+11 = 15 and 7+8 = 15.
21st Apr 2018, 8:17 AM
G.G DOMINOES
+ 5
oh ok, but my languages are web languages. now it's okay. let me time
21st Apr 2018, 8:18 AM
NoxFly
+ 5
your prog works normally, why need you help ?
21st Apr 2018, 8:20 AM
NoxFly
+ 5
I got an exercise and it says that I must use recursion
21st Apr 2018, 8:45 AM
G.G DOMINOES
+ 4
in a time you just execute your function, in a second time you say to output result of your function
21st Apr 2018, 7:50 AM
NoxFly
+ 4
public class SYMM { static int i = 0; static int sum; public static boolean symm(int[] a) { if(i == 0) { sum = a[0] + a[a.length-1]; } if(sum == a[i] + a[a.length-i-1]) { if(i+1 == a.length/2) { return true; } i++; return symm(a); } return false; } public static void main(String[] args) { int[] a1 = {2, 3, 4, 6, 7, 8}; if(symm(a1)) { System.out.println("Yes"); } else{ System.out.println("No"); } } }
21st Apr 2018, 8:09 AM
G.G DOMINOES
+ 4
Because I still didn't understand why is it working when I write "return symm(a);" instead of just calling it simply "symm(a);"
21st Apr 2018, 8:22 AM
G.G DOMINOES
+ 4
oh I understand. why dont use you a for loop ?
21st Apr 2018, 8:36 AM
NoxFly
+ 3
can I have the code please ?
21st Apr 2018, 8:01 AM
NoxFly
+ 3
symm(a) execute the function and return symm(a) return the result of the function (true or false)
21st Apr 2018, 8:24 AM
NoxFly
21st Apr 2018, 8:51 AM
Uttam