Monday, April 1, 2013

The bigger function: two arguments, one result

A function that takes in two numbers and returns the largest value of the two.

/* The bigger function: two arguments, one result */
#include <stdio.h> /* a function taking two double numbers and returning the larger number of the two */ /* this function will return a double */ double bigger (double n1, double n2) { double big; if (n1>n2) big = n1; else big = n2; return (big); } int main (void) { double x, y, result; printf ("Enter a real number: "); scanf ("%lf", &x); printf ("Enter a real number: "); scanf ("%lf", &y); /* calling the function (2 arguments) */ result = bigger (x,y); /* printing the report */ printf ("The bigger number is %lf.\n", result); return (0); }
Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments
Enter a real number: 12.4
Enter a real number: 67.3
The bigger number is 67.300000.

No comments:

Post a Comment