return 0;
} else {
return sum(arr, n-1) + arr[n-1];
}
}
sum([2, 3, 4, 5], 3)
Output: 9 How?
In iteration 1:
if is not satisfied so it goes to else part, now here
sum(arr, n-1) becomes sum([2,3,4,5], 2), where does this go to in next iteration?
arr[n-1] becomes arr[2] which is 4
It's summing 4, 3 and 2
its sum is 9 when sum([2,3,4,5],2) is passed!
Обсуждают сегодня