Link to home
Start Free TrialLog in
Avatar of prasad2315
prasad2315

asked on

how to return multiple values from a function

how to return multiple values from a function
Avatar of ozo
ozo
Flag of United States of America image

You can return a struct
A function can only return one entity. The simplest way handle multiple values to have in/out parameters. In the example below I pass 2 int types to a function by pointer, they are modified in that function and the changes are visible outside of the function.
#include <stdio.h>
 
void foo(int * p1, int * p2)
{
	*p1 = 9;
	*p2 = 3;
}
 
int main(int argc, char* argv[])
{
	int i1, i2;
 
	foo(&i1, &i2);
 
	printf("%d %d", i1, i2);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you can also set global variables, or some cases encode multiple values into a single value