Requires N units for a plus extra space for n, i and sum, so it's O(N).
Because in this question the value of function foo (i) vary between 0 to n so its takes total space n so
Sum = sum + foo(i)
= O(1) + O (N)
= O(N)
so overall space complexity will be O(N)