0

The following recursive function should return 0 if the sum of the provided array is negative, and 1 if the sum is greater than or equal to 0. I don't understand why I get an "error: expected expression before return" when compiling it.

int restituisco(int *p, int len) {

    int somma;

    if (len == 0)
        if (somma < 0)
            return 0;
        else 
            return 1;

    somma = p[len-1] + restituisco(p,--len);
}
Kied Llaentenn
  • 133
  • 1
  • 11
awwww
  • 23
  • 5
  • You can't use `return` as part of an expression. – Nate Eldredge Feb 03 '20 at 14:29
  • okay i changed the function to "int restituisco(int *p, int len) { int somma; if (len == 0) if (somma < 0) return 0; else return 1; somma = p[len-1] + restituisco(p,--len); }" – awwww Feb 03 '20 at 14:32
  • Please edit your question, it's hard to read code in comments. However, note that you are using `somma` before initializing it, which is bad. – Nate Eldredge Feb 03 '20 at 14:34
  • I edited the code in the post. Why the function returns somma instead of returning 1 or 0 depending on if somma is >=0 or less than 0? – awwww Feb 03 '20 at 14:44
  • You need to fix the code so that `somma` has a value when you test it. If we can't tell what you meant, then the compiler certainly can't. – ams Feb 06 '20 at 09:55

0 Answers0