fork download
  1. /* declare function Largest: */
  2. /* The function takes 3 integers as parameters and returns the largest */
  3.  
  4. int Largest (int first, int second, int third);
  5.  
  6. /* now define the function */
  7.  
  8. int Largest (int first, int second, int third)
  9. {
  10. int answer; /* answer will be the largest we find */
  11.  
  12. answer = first; /* assume the first is the largest */
  13.  
  14. if (second > answer) /* if the second number is larger */
  15. answer = second; /* then update the answer */
  16.  
  17. if (third > answer) /* now look at the third number */
  18. answer = third; /* update the answer if we found a greater one */
  19.  
  20. return answer; /* return the answer to the caller */
  21.  
  22. } /* main */
  23.  
  24. /* A test main to show this works */
  25.  
  26. #include <stdio.h>
  27. int main()
  28. {
  29.  
  30. int solution; /* temporary answer from a call */
  31.  
  32. /* Call the function with various sets of test data */
  33.  
  34. solution = Largest (10,20,30); /* find the largest number */
  35. printf ("Largest is %i\n", solution); /* print the answer */
  36.  
  37. solution = Largest (5, 10, 6); /* another test case */
  38. printf ("Largest is %i\n", solution); /* print the answer */
  39.  
  40. return 0; /* exit */
  41. }
Success #stdin #stdout 0s 5308KB
stdin
10, 20, 30
stdout
Largest is  30
Largest is  10