Write a C program to return multiple value from function. How to return more than one value from function in C programming? Different ways to return multiple value from function in C programming.
In previous posts we learned about functions. Function is a set of statements grouped together to perform some specific task. A function can accept any number of parameters (inputs) but can only return (output) single value. However, through the course of programming you will get into situations where you need to return more than one value from a function.
In this post I will explain different ways to return multiple value from a function.
Required knowledge
Functions, Array, Structures, Pointers
Return multiple value from function – using array
If you want to return multiple similar type values from a single function. Then returning an array as a set of values is best suited.
/**
* C program to return multiple value from a function using array.
*/
#include <stdio.h>
#define SIZE 10
/* Function declaration */
int * getNEvenNumbers(const int N, int * numbers);
int main()
{
int evenNumbers[SIZE];
int i;
// Call function to get first 10 even numbers
getNEvenNumbers(SIZE, evenNumbers);
// Print all numbers
printf("First %d even numbers are: \n", SIZE);
for (i = 0; i < SIZE; i++)
{
printf("%d ", *(evenNumbers + i));
}
return 0;
}
/**
* Function to get N even numbers.
*/
int * getNEvenNumbers(const int N, int * numbers)
{
int i;
for (i = 0; i < N; i++)
{
// Calculate and store even number in numbers
*(numbers + i) = 2 * (i + 1);
}
return numbers;
}
Output
First 10 even numbers are: 2 4 6 8 10 12 14 16 18 20
Return multiple value from function – using structure
Not always you will return similar types from a function. There are situations when you need to return multiple values of multiple types from a function. You can group all these values together in a structure and can return from function.
The basic idea is to return single structure variable. The structure itself will store multiple values.
/**
* C program to return multiple value from a function using structure.
*/
#include <stdio.h>
#define MAX_SIZE 10
typedef struct MinMax
{
int min;
int max;
}MinMax;
/* Function declaration */
MinMax getMinMax(int * array, const int SIZE);
int main()
{
int array[MAX_SIZE];
int N, i;
MinMax arrayMinMax;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Enter %d elements in array: ", N);
for (i = 0; i < N; i++)
{
scanf("%d", (array + i));
}
arrayMinMax = getMinMax(array, N);
printf("Minimum value in array : %d \n", arrayMinMax.min);
printf("Maximum value in array : %d \n", arrayMinMax.max);
return 0;
}
/**
* Function to find minimum and maximum value in array.
*
* @returns The function returns a struct object containing
* minimum and maximum value in array.
*/
MinMax getMinMax(int * numbers, const int SIZE)
{
int i;
int min = *(numbers + 0);
int max = *(numbers + 0);
MinMax arrayMinMax;
// Find minmum and maximum value
for (i = 0; i < SIZE; i++)
{
if(*(numbers + i) < min)
min = *(numbers + i);
if(*(numbers + i) > max)
max = *(numbers + i);
}
// Copy minimum and maximum vaue to return object.
arrayMinMax.min = min;
arrayMinMax.max = max;
return arrayMinMax;
}
Output
Enter size of array: 5 Enter 5 elements in array: 1 -2 3 -1 9 Minimum value in array : -2 Maximum value in array : 9
Return multiple value from function – using pointers
In C you can pass parameters to a function either by value or by reference. Changes made to variables passed by reference persists after the function. Hence you can pass any number of parameters as reference, which you want to return from function.
/**
* C program to return multiple value from function using pointers
*/
#include <stdio.h>
#define MAX_SIZE 100
/* Function declaration */
void getMinMax(int * numbers, int size, int * min, int * max);
int main()
{
int arr[MAX_SIZE], size, i;
int min, max;
/*
* Input size and elements in array
*/
printf("Enter size of array: ");
scanf("%d", &size);
printf("Enter %d elements in array: ", size);
for (i = 0; i < size; i++)
{
scanf("%d", (arr + i));
}
// Call min max function to get minimum and maximum value.
getMinMax(arr, size, &min, &max);
printf("Minimum element in array: %d\n", min);
printf("Maximum element in array: %d\n", max);
return 0;
}
/**
* Function to get minimum and maximum element in array.
*
* @numbers Array in which we need to find max and min.
* @size Size of the array.
* @min Pointer to integer where minimum element is to be stored.
* @max Pointer to integer where maximum element is to be stored.
*/
void getMinMax(int * numbers, int size, int * min, int * max)
{
int i;
*min = *(numbers + 0);
*max = *(numbers + 0);
for (i = 0; i < size; i++)
{
// Check minimum number
if(*(numbers + i) < *(min))
*min = *(numbers + i);
// Check maximum number
if(*(numbers + i) > *(max))
*max = *(numbers + i);
}
}
Output
Enter size of array: 10 Enter 10 elements in array: -1 0 100 4 9 18 94 -35 10 20 Minimum element in array: -35 Maximum element in array: 100