I am learning recursion and I have doubts about using global variables, particularly I think a clumsy gambiarra , maybe you could be wrong. I made a code to add positive numbers and I used a variable called sum. I wonder if there are any other ways out of this question. Code below:
#include<stdio.h>
int soma = 0;
int SomaPositivos(int vet[], int n) {
if (n == 0) {
return 0;
} else {
int aux;
if (vet[n-1] > 0) {
aux = vet[n-1];
soma = soma + aux;
SomaPositivos(vet, n-1);
}
}
return soma;
}
int main () {
int v[20] = {2, 1, 8, 3, 4};
int a;
a = SomaPositivos(v, 5);
printf("%d ", a);
return 0;
}