Example 1: Utilizing the Tuples/Pairs
Let’s do some practical demonstration to return multiple values in C++. Here, we utilize the tuples/pairs techniques to help return multiple values in our code. C++ provides various header files which we have to include in our code. We include “bits/stdc++.h” here as it contains all libraries of the C++ programming. Then, the namespace is added here which is “std”. After this, we utilize the “tuple” keyword in which we place three data types, two float data types, and the remaining one “char” data type. Below it, we utilize the “return” keyword to pack the values to return a tuple.
Now, we utilize the “pair” method to return multiple values. In this “pair” method, we put two variable’s data types, and both are “float” here. The variables are named “float_1” and “float_2”. Then, we pack two values to return to the “pairs”. After this, we invoke the “main()” here and then declare two “float” variables with the “f_1, f_2” name. The “char” variable is also declared here as “myChar”. Then, we unpack the values that are returned by the “My_Tuple()” function.
Below this, we store the returned values in pairs. We store ”4.99, 8.98” in the “My_Tuple” function and “6.86, 3.22” in the “My_Pair” function. Then, we utilize the “cout” that prints all the information that are written inside it.
Code 1:
using namespace std;
tuple<float, float, char> My_Tuple(float f_1, float f_2){
return make_tuple(f_2, f_1, '$');
}
pair<float, float> My_Pair(float f_a, float f_b){
return make_pair(f_b, f_a);
}
int main(){
float f_1,f_2;
char myChar;
tie(f_1, f_2, myChar) = My_Tuple(4.99, 8.98);
pair new_p = My_Pair(6.86, 3.22);
cout << "Values we get by tuples : ";
cout << f_1 << " " << f_2 << " " << myChar << endl;
cout << "Values we get by Pair: ";
cout << new_p.first << " " << new_p.second;
return 0;
}
Output:
The values that we get here by utilizing the “tuples” and the “pair” method are displayed in the following. Note that it returns multiple values here.
Example 2: Utilizing the Pointers
We pass the parameters along with their addresses in the “compare ” function here. We add “value_1” and “value_2” of the “int” type and the “int* g_Address, int* s_Address”. After this, we utilize the “if” condition in which we add a condition that the “value_1” is greater than “value_2”. If this is satisfied, the following statement is executed. If it is not, it ignores the statement that is added below this and move towards the “else” part. Now, after invoking the “main()”, we declare four new variables with the names “g_value”, “s_value”, “newValue1”, and “newValue2”.
After this, we print a message to enter the numbers and then place “cin” which gets two values from the user. The values that the user enters are saved in the “newValue1” and “newValue2” variables, respectively. After this, we call the “compare()” function which we previously created and pass four parameters into it. Then, we display the result after performing the “compare” function and it shows the greater number and the smaller number from those numbers that the user enters.
Code 2:
using namespace std;
void compare(int value_1, int value_2, int* g_Address, int* s_Address)
{
if (value_1 > value_2) {
*g_Address = value_1;
*s_Address = value_2;
}
else {
*g_Address = value_2;
*s_Address = value_1;
}
}
int main()
{
int g_value, s_value, newValue_1, newValue_2;
cout << "Please Enter two numbers: " <> newValue_1 >> newValue_2;
compare(newValue_1, newValue_2, &g_value, &s_value);
cout << "\nThe greater number is " << g_value << " and the smaller number is "
<< s_value;
return 0;
}
Output:
The user enters “86” and “23” here. After pressing “Enter”, it displays the result. In this way, we get multiple values.
Example 3: Utilizing the Array
We create the “ComputeComparison()” function here in which we insert two variables, “num_1” and “num_2”, as the “int” type and also an array named “my_arr[]”. After this, we have the “if” condition which checks whether “num_1” is greater than “num_2” or not. If it is true, the “num_1” is assigned to “my_arr[0]” and “num_2” is assigned to “my_arr[1]”. But if the condition is not true, the statements after “else” are executed in which we assign “num_2” to “my_arr[0]” and “num_1” to “my_arr[1]”.
After this, we call the “main()” here and then declare two more int variables: “newNum_1” and “newNum_2”. After this, an array of size “2” is declared. After this, we get two numbers from the user with the help of “cin” and then call the “ComputeComparison()” function and display the following result. So, it returns multiple values here.
Code 3:
using namespace std;
void ComputeComparison(int num_1, int num_2, int my_arr[])
{
if (num_1 > num_2) {
my_arr[0] = num_1;
my_arr[1] = num_2;
}
else {
my_arr[0] = num_2;
my_arr[1] = num_1;
}
}
int main()
{
int newNum_1, newNum_2;
int my_arr[2];
cout << "Please Enter two numbers for comparison " <> newNum_1 >> newNum_2;
ComputeComparison(newNum_1, newNum_2, my_arr);
cout << "\nThe greater number is " << my_arr[0] << " and the "
"smaller number is " << my_arr[1];
return 0;
}
Output:
We type both “54” and “98” here and then press “Enter” to display the result. It shows the greater as well as the smaller numbers from the numbers that we entered.
Example 4: Utilizing the Tuples
Two header files are included here: “tuple” and “iostream”. Next, the “std” namespace is put here. Next, we use the “tuple” keyword and insert two data types which are “int”. After this, we create a function with the “findingValues()”name and pass “intValue_1” and “intValue2” as its parameters.
Then, the “if” is placed where we type the “intValue_1 < intValue_2” condition. Below it, we utilize the “return” keyword and place the “make_tuple()” function in which both variables are added as the “intValue_1, intValue2_” parameter. Then, we have the “else” part in which we place “return” again along with the “make_tuple()” function. But here, we place the “intValue_2” first and then the “intValue1”. Now, we call the “main()” and initialize the “new_value1” with “5” and “new_value2” with “28”.
In the following, we declare two more variables of type “int” with the names “greater” and “smaller”. Then, we place the “tie()” function and pass the “smaller, greater” variables as the parameter and also call the “findingValues()” function here. After this, we print both values: the greater and the smaller numbers.
Code 4:
#include<tuple>
using namespace std;
tuple findingValues(int intValue_1, int intValue_2)
{
if (intValue_1 < intValue_2) {
return make_tuple(intValue_1 , intValue_2);
}
else {
return make_tuple(intValue_2 , intValue_1);
}
}
int main()
{
int new_value1 = 5, new_value2= 28;
int greater, smaller;
tie(smaller, greater) = findingValues(new_value1, new_value2);
printf("The greater number is %d and the "
"smaller number is %d",
greater, smaller);
return 0;
}
Output:
The numbers that we add to our code simultaneously display the greater and smaller values. In this way, we can easily return multiple values in our code.
Conclusion
This guide is about the “returning multiple values” in C++ codes. We thoroughly explored this notion in this guide and discussed three techniques that aid in returning multiple values in the C++ programming. We explained that multiple values are returned by utilizing the tuples, pairs, pointers, and array techniques. All these techniques are thoroughly described here.