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.
rnFor example, declaration of a function is given below:
rnint xyz(int abc1, int abc2);
rnFrom this, we can see that our interface to the function is through arguments and return value only.
rnBut can return value be of pointer type? Yes, even though a function can return only one value, that value can be of pointer type.
rnWe 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.
rnFor the function to return multiple values of same data types, we should return the pointer to the array of that data types.
rnWe 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.
rnUsually, when a function needs to return several values, we use one pointer in return instead of several pointers as arguments.