How can I return multiple values from a function?
In C programming, a function can return only one value directly. However, C also provides several indirect methods in to return multiple values from a function. In this article, we will learn the different ways to return multiple values from a function in C.
The most straightforward method to return multiple values from a function is by using a structure. Let's take a look at an example:
#include <stdio.h>
// Structure of multiple values
struct A {
int a;
char c;
};
// Function that returns struct A
struct A func() {
struct A r;
r.a = 10;
r.c = 'z';
// Return the struct
return r;
}
int main() {
// Storing the returned structure
struct A res = func();
printf("a = %d, c = %c\n", res.a, res.c);
return 0;
}
// Structure of multiple values
struct A {
int a;
char c;
};
// Function that returns struct A
struct A func() {
struct A r;
r.a = 10;
r.c = 'z';
// Return the struct
return r;
}
int main() {
// Storing the returned structure
struct A res = func();
printf("a = %d, c = %c\n", res.a, res.c);
return 0;
}
Output
a = 10, c = z
Explanation: In the above program, structure A with multiple members is used returning multiple values from the function func(). In main() function, the returned structure is stored, and its values are printed.
C also have some other methods to return multiple value from a function. They are as follows:
Using Arrays
If the multiple values to be returned are of same type, then the array of these values can be returned. But it is to be noted that the array should be either declared as static or should be dynamically allocated so that they don't get destroyed when the function ends.
#include <stdio.h>
int* func() {
// Creating static array
static int arr[2] = {10, 20};
// Returning multiple values using static array
return arr;
}
int main() {
// Store the returened array
int* arr = func();
printf("%d %d", arr[0], arr[1]);
return 0;
}
int* func() {
// Creating static array
static int arr[2] = {10, 20};
// Returning multiple values using static array
return arr;
}
int main() {
// Store the returened array
int* arr = func();
printf("%d %d", arr[0], arr[1]);
return 0;
}
Output
10 20
Explanation: In this case, an array values[] is passed to the function getValues, which assigns values to the array. Since arrays in C are passed by reference, the changes made inside the function affect the original array.
Using Pointers
This method is technically not for returning but writing the result of the function in the address (pointers) already passed as parameters. It is an alternative to returning structures or arrays.
#include <stdio.h>
void foo(int* a, char* c) {
// Writing data to passed addresses
*a = 20;
*c = 'z';
}
int main() {
int a;
char c;
foo(&a, &c);
printf("a = %d, c = %d", a, c);
return 0;
}
void foo(int* a, char* c) {
// Writing data to passed addresses
*a = 20;
*c = 'z';
}
int main() {
int a;
char c;
foo(&a, &c);
printf("a = %d, c = %d", a, c);
return 0;
}
Output
a = 20, c = 122
We can also pass arrays in place of seperate variable addresses or even use global variable for storing the results.