This is How to return multiple values from a function in C or C++? coding technolo
|Y
New programmers are normally within the search of how to return a number of values from a perform. Unfortunately, C and C++ don’t enable this straight. But fortuitously, with a bit of little bit of intelligent programming, we will simply obtain this.
Below are the strategies to return a number of values from a perform in C:
- By using pointers.
- By using constructions.
- By using Arrays.
Example: Consider an instance the place the duty is to seek out the higher and smaller of two distinct numbers. We may write a number of capabilities. The main drawback is the difficulty of calling multiple capabilities since we have to return a number of values and naturally, having extra variety of strains of code to be typed.
- Returning a number of values Using pointers: Pass the argument with their deal with and make modifications of their worth using pointer. So that the values get turned into the unique argument.
#include <stdio.h>
void
compare(
int
a,
int
b,
int
* add_great,
int
* add_small)
{
if
(a > b) {
*add_great = a;
*add_small = b;
}
else
{
*add_great = b;
*add_small = a;
}
}
int
main()
{
int
great, small, x, y;
printf
(
"Enter two numbers: n"
);
scanf
(
"%d%d"
, &x, &y);
compare(x, y, &great, &small);
printf
(
"nThe greater number is %d and the"
"smaller number is %d"
,
great, small);
return
0;
}
Output:
Enter two numbers: 5 8 The higher quantity is 8 and the smaller quantity is 5
- Returning a number of values using constructions : As the construction is a user-defined datatype. The thought is to outline a construction with two integer variables and retailer the higher and smaller values into these variable, then use the values of that construction.
#include <stdio.h>
struct
greaterSmaller
int
greater, smaller;
;
typedef
struct
greaterSmaller Struct;
Struct findGreaterSmaller(
int
a,
int
b)
{
Struct s;
if
(a > b)
else
s.
greater = b;s.smaller = a;
return
s;
}
int
main()
Output:
Enter two numbers: 5 8 The higher quantity is 8 and the smaller quantity is 5
- Returning a number of values using an array (Works solely when returned gadgets are of identical sorts): When an array is handed as an argument then its base deal with is handed to the perform so no matter modifications made to the copy of the array, it’s modified within the unique array.
Below is this system to return a number of values using array i.e. retailer higher worth at arr[0] and smaller at arr[1].#include <stdio.h>
void
findGreaterSmaller(
int
a,
int
b,
int
arr[])
{
if
(a > b) {
arr[0] = a;
arr[1] = b;
}
else
{
arr[0] = b;
arr[1] = a;
}
}
int
main()
{
int
x, y;
int
arr[2];
printf
(
"Enter two numbers: n"
);
scanf
(
"%d%d"
, &x, &y);
findGreaterSmaller(x, y, arr);
printf
(
"nThe greater number is %d and the"
"smaller number is %d"
,
arr[0], arr[1]);
return
0;
}
Output:
Enter two numbers: 5 8 The higher quantity is 8 and the smaller quantity is 5
C++ Only Methods
- Returning a number of values Using References: We use references in C++ to retailer returned values.
#include <stdio.h>
void
compare(
int
a,
int
b,
int
&add_great,
int
&add_small)
{
if
(a > b) {
add_great = a;
add_small = b;
}
else
{
add_great = b;
add_small = a;
}
}
int
main()
{
int
great, small, x, y;
printf
(
"Enter two numbers: n"
);
scanf
(
"%d%d"
, &x, &y);
compare(x, y, great, small);
printf
(
"nThe greater number is %d and the"
"smaller number is %d"
,
great, small);
return
0;
}
Output:
Enter two numbers: 5 8 The higher quantity is 8 and the smaller quantity is 5
- Returning a number of values using Class and Object : The thought is much like constructions. We create a category with two integer variables and retailer the higher and smaller values into these variable, then use the values of that construction.
#include <stdio.h>
class
GreaterSmaller
public
:
int
higher, smaller;
;
GreaterSmaller findGreaterSmaller(
int
a,
int
b)
{
GreaterSmaller s;
if
(a > b)
else
s.
greater = b;s.smaller = a;
return
s;
}
int
main()
int
x, y;
GreaterSmaller result;
printf
(
"Enter two numbers: n"
);
scanf
(
"%d%d"
, &x, &y);
result = findGreaterSmaller(x, y);
printf
(
"nThe greater number is %d and the"
"smaller number is %d"
,
result.higher
greater, result.smaller);return
0;
Output:
Enter two numbers: 5 8 The higher quantity is 8 and the smaller quantity is 5
- Returning a number of values using STL tuple : The thought is much like constructions. We create a tuple with two integer variables and return the tuple, after which inside main perform we use tie perform to assign values to min and max that’s returned by the perform.
#include<iostream>
#include<tuple>
using
namespace
std;
tuple <
int
,
int
> find Smaller(
int
a,
int
b)
{
if
(a < b) {
return
make_tuple(a, b);
}
else
{
return
make_tuple(b, a);
}
}
int
main()
{
int
x = 5, y= 8;
int
max, min;
tie(min, max) = findGreaterSmaller(x, y);
printf
(
"The greater number is %d and the "
"smaller number is %d"
,
max, min);
return
0;
}
Output:
The higher quantity is 8 and the smaller quantity is 5