Firstly, it is poorly encoded :)
double foo (int n) { // foo return a double, and takes an integer parameter int i; // declare an integer variable i, that is used as a counter below double sum; // this is the value that is returned if (n==0) return 1.0; // if someone called foo(0), this function returns 1.0 else { // if n != 0 sum = 0.0; // set sum to 0 for (i =0; i<n; i++) // recursively call this function n times, then add it to the result sum +=foo(i); return sum; // return the result } }
You call foo () as a whole, something like n ^ n (where you round n to the nearest integer)
eg:.
foo (3) will be called 3 ^ 3 times.
Good luck and merry christmas.
EDIT: oops, just adjusted. Why does foo return double? It will always return an integer, not a double.
Here would be the best version with micro-optimization !: D
int foo(int n) { if(n==0) return 1; else{ int sum = 0; for(int i = 0; i < n; ++i) sum += foo(i); return sum; } }
source share