1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include<stdio.h>
#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 |