C and C + + don't allow you to return multiple values. For example, as I told previously that we cannot return an array as an argument, that means we are not allowed to return multiple values in C and C++.
How to return multiple values in C++?
Now we know that we are not allowed to do that so, what is the right way of doing that? Even if we are not allowed to do that there are some programming tricks by using them we can easily return what we want for whatever reason.
Methods
- Pointers
- Structures
- References
- Classes
title: "How to return multiple values in C++?"
tags: cpp
canonical_url: https://kodlogs.com/blog/326/how-to-return-multiple-values-in-c
Assuming an example where we have to find the average of smallest and the largest of two values. We can use functions but the problem begins with the calling the function we have to call it multiple times.
Using pointers
We can pass the address instead of passing the function multiple times.
// Modified program using pointers
#include <stdio.h>
#include<iostream>
using namespace std;
// add is the short name for address
void compare(int a, int b, int* add_great, int* add_small)
{
if (a > b) {
// a is stored in the address pointed
// by the pointer variable *add_great
*add_great = a;
*add_small = b;
}
else {
*add_great = b;
*add_small = a;
}
}
// Driver code
int main()
{
int great, small, x, y;
float result;
printf("Enter two numbers: \n");
cin >> x >> y;
// The last two arguments are passed
// by giving addresses of memory locations
compare(x, y, &great, &small);
result = (great + small)*0.5;
cout << "the average of numbers is " << result;
return 0;
}
Using structures
The structure is not a fundamental data type it is a derived data type or you can say user-defined data types. Here we will be defining two variables and store the values into those variables. Then we will be using that structure.
// Modified program using structures
#include <stdio.h>
#include <iostream>
using namespace std;
struct greaterSmaller {
int greater, smaller;
};
typedef struct greaterSmaller Struct;
Struct findGreaterSmaller(int a, int b)
{
Struct s;
if (a > b) {
s.greater = a;
s.smaller = b;
}
else {
s.greater = b;
s.smaller = a;
}
return s;
}
// Driver code
int main()
{
int x, y;
Struct result;
printf("Enter two numbers: \n");
scanf("%d%d", &x, &y);
// The last two arguments are passed
// by giving addresses of memory locations
result = findGreaterSmaller(x, y);
cout << "\n The average of numbers is " << (result.greater + result.smaller)*0.5;
return 0;
}
Using references
Using references to store returned values.
// Modified program using References in C++
#include <stdio.h>
#include<iostream>
using namespace std;
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;
}
}
// Driver code
int main()
{
int great, small, x, y;
printf("Enter two numbers: \n");
scanf("%d%d", &x, &y);
// The last two arguments are passed
// by giving addresses of memory locations
compare(x, y, great, small);
cout << "The average of numbers is " << (great + small)*0.5;
return 0;
}
Using classes
The logic behind using classes is the same as the structures. We will be creating a class with two variables and those variables will be storing values. Then we will be using that.
// Modified program using class
#include <stdio.h>
#include<iostream>
using namespace std;
class GreaterSmaller {
public:
int greater, smaller;
};
GreaterSmaller findGreaterSmaller(int a, int b)
{
GreaterSmaller s;
if (a > b) {
s.greater = a;
s.smaller = b;
}
else {
s.greater = b;
s.smaller = a;
}
return s;
}
// Driver code
int main()
{
int x, y;
GreaterSmaller result;
printf("Enter two numbers: \n");
scanf("%d%d", &x, &y);
// The last two arguments are passed
// by giving addresses of memory locations
result = findGreaterSmaller(x, y);
cout << "The average of numbers is " << (result.greater + result.smaller)*0.5;
return 0;
}
All the codes are working, you can test them in your IDE for yourself. If you have any queries you can ask in comment section.
Top comments (0)