Return Multiple Values from a Function in C

Return Multiple Values from a Function in C

How to Return Multiple Values from a Function?

rn

We all know that a function in C can return only one value. But, how do we achieve the purpose of returning multiple values from a function in C? We can declare the function such that, it returns a structure type user-defined variable or a pointer to it. 

rn

For example, declaration of a function is given below:

rn

int xyz(int abc1, int abc2);

rn

From this, we can see that our interface to the function is through arguments and return value only. 

rn

But can return value be of pointer type? Yes, even though a function can return only one value, that value can be of pointer type. 

rn

We can declare the function such that, it returns a structure type user-defined variable or a pointer to it. And by the property of a structure, we know that a structure in C can hold multiple values of asymmetrical types, i.e. one int variable, four char variables, two float variables, etc.

rn

For the function to return multiple values of same data types, we should return the pointer to the array of that data types.

rn

We can also make the function return multiple values by using the arguments of the function. We can achieve this by providing the pointers as arguments.

rn

Usually, when a function needs to return several values, we use one pointer in return instead of several pointers as arguments.

c