Puzzle SL45

Function

It would be nice to implement the sum in a function. Now the function can be reused with different limits.


 
/* --- sumVersion5.c --- */
#include <stdio.h>
 
int sumInts( int limit )
{
  int j;
  int sum = 0;
  
  for ( j=1; j<=limit; j++ )
    sum += j;
    
  return sum;
}
 
void main()
{   
  int sum = sumInts( 10 );
  printf( "sum of 1..10: %d\n", sum );    

  sum = sumInts( 5 );
  printf( "sum of 1..5: %d\n", sum );    
   
}

What does the code write to the monitor?



Previous Page        Answer         Next Page         Home