I'm trying to print out the result of my returned sum but for some reason it prints out 0, but when I check the sum inside the function its performing its objective correctly. For example if I pass in an array of 10 items each equal to their index (0...9) the sum inside the function is 45 but when I print it out back in main() it prints 0. I'm really confused to say the least.
Here is my function, which I call using:
printf("%d\n", addArray3(arrayA, 9, 0));
To clarify, arrayA is a pointer to an 10 item'd array of integers.
// Sums array using recursion
int addArray3(int (*arrayA)[], int n, int sum){
if (n<0){
return sum;
} else {
sum += (*arrayA)[n];
}
addArray3(arrayA, --n, sum);
}
If anyone can clear this up I'd really appreciate it, thanks!