### What does this function do ?

When executed the program outputs :

The result is 55.

I'm not sure how that result was processed any help or hints would be much appreciated. Thank you for your time in advanced.

 ``1234567891011121314151617181920212223242526`` ``````#include #define SIZE 10 int whatIsThis(const int b[], size_t p);//function prototype //function main begins program execution int main (void) { int x; //holds return value of function whatIsThis //initialize array a int a[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; x = whatIsThis(a, SIZE); printf("Result is %d\n", x); } // what does this function do? int whatIsThis(const int b[], size_t p) { //base case if (1 == p){ return b[0]; } else{//recursion step return b[p-1]+whatIsThis(b, p-1); } }``````
Last edited on
If the condition at line 20 is false - which it is, in the call on line 13, then you don't have a return statement, so you have undefined behaviour. That's why you're getting a weird number.

 ``12345678910111213141516171819202122232425262728`` ``````#include #define SIZE 10 int whatIsThis(const int b[], size_t p);//function prototype //function main begins program execution int main (void) { int x; //holds return value of function whatIsThis //initialize array a int a[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; x = whatIsThis(a, SIZE); printf("Result is %d\n", x); } // what does this function do? int whatIsThis(const int b[], size_t p) { //base case if (1 == p){ printf("%d \n ", b[0]); return b[0]; } else{//recursion step printf("%d + ", b[p-1]); return b[p-1]+whatIsThis(b, p-1); } }`````` ```10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 Result is 55```