A function that return a value from multiple values to the calling program/function in C programming
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: A function that return a value from multiple values to the calling program/function in C programming
To show: How to return a value from multiple values processed by C function
// Using conditional return statement in a function
#include <stdio.h>
// Function prototype
int larger_of(int, int);
void main(void)
{
int x, y, z;
puts("Enter two different integer values,");
puts("separated by space. Then press Enter: ");
/* scanf("%d%d", &x, &y); */
// read and store
scanf_s("%d%d", &x, &y, 1, 1);
// function call with two arguments
z=larger_of(x, y);
printf("\nThe larger value is %d.", z);
printf("\n");
}
// Function definition, receive two integers and return an integer
int larger_of(int a, int b)
{
// return a or b, not both
if(a > b)
return a;
else
return b;
}
Output example:
Enter two different integer values,
separated by space. Then press Enter:
31 13
The larger value is 31.
Press any key to continue . . .