Introduction
A function is a collection of statements that work together to complete a job. Every C program contains at least one function, main(), and even the most straightforward programs can specify more functions.
When a function completes its task, it returns a value to the script or function that called it.
A function in C can return only one value. In this section, we will see how can we return multiple values from a function.
Also See, Sum of Digits in C and C Static Function.
Return multiple values from a function in C
We already know that the design of C functions prohibits us from returning multiple values. However, programmers frequently want a function to return multiple values. Fortunately, there are a few solutions for returning multiple values in C.
The methods for returning multiple values from a function in C are as follows:
- Using pointers.
- Using structures.
- Using Arrays.
Let us look at each method separately with examples
Using Pointers
In C, we may employ pointers to return more than one value from a function by passing pointers as function arguments and using them to set several values visible in the caller function.
Example
#include <stdio.h>
// Function to return multiple values using pointers
void func(int *var1, int *var2, char *var3)
{
*var1 = 40;
*var2 = 50;
*var3 = 'X';
}
int main(void)
{
int var1, var2;
char var3;
func(&var1, &var2, &var3);
printf("var1 = %d, var2 = %d, var3 = %c", var1, var2, var3);
return 0;
}
Output
var1 = 40, var2 = 50, var3 = X
Using Structures
Structures in C can also return more than one value from a function. In C, a structure is a user-defined datatype that may store multiple data types of the same or different types.
The objective is to construct a struct with all essential data types as members and return that struct from our function. Then, we can obtain the desired data from the struct within our caller function.
Example
#include <stdio.h>
// wrap the multiple values into a struct
struct Tuple
{
int var1, var2;
char var3;
};
// Function to return multiple values using struct
struct Tuple struc()
{
struct Tuple vars = {40, 50, 'X'};
return vars;
}
int main(void)
{
int var1, var2;
char var3;
struct Tuple vars = struc();
var1 = vars.var1;
var2 = vars.var2;
var3 = vars.var3;
printf("var1 = %d, var2 = %d, var3 = %c", var1, var2, var3);
return 0;
}
Output
var1 = 40, var2 = 50, var3 = X
Using Arrays
We've seen how to use pointers and structures to return values of different data types from a function. If all of the values are of the same data type, we may wrap them in an array and return it, as seen below:
Example
#include <stdio.h>
#include <stdlib.h>
// Function to return multiple values using an array
int *func()
{
// Dynamically allocate memory
int *tempVar = (int *)malloc(sizeof(int) * 3);
*tempVar = 40;
*(tempVar + 1) = 50;
*(tempVar + 2) = 60;
return tempVar;
}
int main(void)
{
int var1, var2, var3;
int *arr = func();
var1 = arr[0];
var2 = arr[1];
var3 = arr[2];
printf("var1 = %d, var2 = %d, var3 = %d", var1, var2, var3);
return 0;
}
Output
var1 = 40, var2 = 50, var3 = 60
This method should not be used since the variable information is not passed to the caller function. For example, we're retrieving the values of our variables by using the array's index. Also, keep in mind that the array must be allocated dynamically in a heap. After employing the static array, it disappears when we exit the method, and accessing it within the caller function results in undefined behaviors.
Also see, Tribonacci Series and Short int in C Programming