fork download
  1. #include <iterator>
  2. #include <iostream>
  3.  
  4. int sum(int* a, unsigned size, int acc = 0)
  5. {
  6. if ( size == 0 )
  7. return acc ;
  8. else
  9. return sum(a+1, size-1, acc + *a);
  10. }
  11.  
  12. int main()
  13. {
  14. int array[] = { 1, 2, 3, 4, 5, 6 } ;
  15. unsigned array_size = std::end(array) - std::begin(array) ;
  16.  
  17. std::cout << sum(array, array_size) << " is the sum of elements in array.\n" ;
  18. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
21 is the sum of elements in array.