IHYPRESS.NET > C PROGRAMMING
HOME | CLASSIC ASP | C | HTML/CSS | JAVASCRIPT | PERL | PHP | PYTHON | RUBY
C > 06. Functions II (Pointers) [PROGRAM #02]
CODERESULT
<
/* Problem: Write a void function that "returns" the largest
value of two real numbers. */

/* this function returns nothing but will
change the value of the variable in the calling
function's memory area */

void
bigger2 (double n1, double n2, double *result)
{

	if (n1>n2)
		*result = n1;
	else
		*result = n2;

/* *result in bigger2 uses the same 
memory cell as result in main */
}



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 (3 arguments) */
	/* &result is the address where variable result is stored */
	bigger2 (x, y, &result);

	/* printing the report */
	printf ("The largest number is %f.\n", result);	

	return (0);
}
Legend: preprocessor directives | variable declarations main function | helper functions | user-defined structures comments
Enter a real number: 12.4
Enter a real number: 67.3
The largest number is 67.300000.
>
COPYRIGHT © 2021 IHYPRESS.NET. A DIVISION OF IHY PRESS, KIRKLAND, WA 98083 USA. ALL RIGHTS RESERVED.