-1

I'm trying to create a function read() and display(). They should read 3 values and print those 3 values using another function which is the display(). I tried doing this:

#include<iostream>
int read(void);
void display(int n1, int n2, int n3);
using namespace std;
int main()
{
    int n1, n2, n3;
    n1, n2, n3 = read();
    print(n1,n2,n3);
    reverse(n1,n2,n3);
    
    return 0;    
}

int read(void)
{
    int n1, n2, n3;
    
    cout << "Enter three numbers: ";
    cin >> n1;
    cin >> n2;
    cin >> n3;
    return n1, n2, n3;
}

void display(int n1, int n2, int n3)
{
    cout << n1 << " " << n2 << " " << n3 << endl;
    return;
}

It only stored the n3 value. Is there another way I can go about this?

Alex
  • 11
  • 2
  • 2
    You cannot return three values from a function in C/C++. Either pass the calculated values via pointers or create and return a dynamic array. – DYZ Nov 28 '20 at 23:27
  • 1
    It looks like you're used to Python or something like it. If you want to return multiple values in C++, a little more work is required. Take a look at [std::tuple](https://en.cppreference.com/w/cpp/utility/tuple) and [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding) for this case. – Nathan Pierson Nov 28 '20 at 23:28
  • To return multiple values and then assign them to multiple values you can use structured binding https://en.cppreference.com/w/cpp/language/structured_binding – QuentinUK Nov 28 '20 at 23:32

2 Answers2

2

C++ does not allow multiple retruns like you're attempting. There are multiple ways of doing this.


Solution one: passing pointers to values and setting them in read function:

#include <iostream>

using namespace std;

void read(int* n1, int* n2, int* n3);
void display(int n1, int n2, int n3);

int main()
{
    int n1, n2, n3;
    read(&n1, &n2, &n3);
    display(n1, n2, n3);
    //print(n1,n2,n3);
    //reverse(n1,n2,n3);

    return 0;
}

void read(int* n1, int* n2, int* n3)
{
    cout << "Enter three numbers: ";
    cin >> *n1;
    cin >> *n2;
    cin >> *n3;
}

void display(int n1, int n2, int n3)
{
    cout << n1 << " " << n2 << " " << n3 << endl;
    return;
}

Alternatively, for this same method, you can deal with references instead of pointers, in C++, passing by reference can apply to all types, not just primitive types. It would look like this:

void read(int &n1, int &n2, int &n3)
{
    cout << "Enter three numbers: ";
    cin >> n1;
    cin >> n2;
    cin >> n3;
}

The function call it main would become:

read(n1, n2, n3);

Note that it is crucial the & is present in read function signature to make sure you pass by reference and not by value. Passing by value will not reflect on the original passed parameters.

This has the same effect as passing by pointers, but it looks much cleaner and it does not require you use *.


Solution two: declaring them global within the cpp file. I do not recommend this solution because it's better that functions carry their own data without relying too much on global variables, unless there is no other way. Generally, classes/structs are better for this.

#include<iostream>

void read(void);
void display();

int n1, n2, n3;  // declared here

using namespace std;

int main()
{
    read();
    display();
    //print(n1,n2,n3);
    //reverse(n1,n2,n3);
    
    return 0;    
}

void read(void)
{
    cout << "Enter three numbers: ";
    cin >> n1;
    cin >> n2;
    cin >> n3;
}

void display()
{
    cout << n1 << " " << n2 << " " << n3 << endl;
    return;
}

Solution three: having a data structure to hold the values and dealing with it instead. In this approach, we put n1 n2 n3 as part of a struct and get it from/to functions.

#include<iostream>

struct data{
    int n1; 
    int n2;
    int n3; 
}

data read(void);
void display(data);



using namespace std;

int main()
{
    data d = read();
    display(d);
    //print(n1,n2,n3);
    //reverse(n1,n2,n3);
    
    return 0;    
}

data read(void)
{
    data d; 
    cout << "Enter three numbers: ";
    cin >> d.n1;
    cin >> d.n2;
    cin >> d.n3;
    return d;
}

void display(data d)
{
    cout << d.n1 << " " << d.n2 << " " << d.n3 << endl;
    return;
}

As others have suggested in the comments, you can also use std::tuple to return multiple values, it works similar to how data struct is used in solution three, and it is the solution I would recommend the most for your case:

#include <iostream>
#include <tuple>

using namespace std;

tuple<int, int, int> read(void);
void display(tuple<int, int, int>);




int main()
{
    tuple<int, int, int> d = read();
    display(d);
    //print(n1,n2,n3);
    //reverse(n1,n2,n3);
    
    return 0;    
}

tuple<int, int, int> read(void)
{       
    int n1, n2, n3; 
    
    cout << "Enter three numbers: ";
    cin >> n1;
    cin >> n2;
    cin >> n3;
    
    return std::make_tuple(n1, n2, n3);
}

void display(tuple<int, int, int> d)
{
    cout << std::get<0>(d) << " " << std::get<1>(d) << " " << std::get<2>(d) << endl;
    return;
}
Everyone
  • 1,751
  • 13
  • 36
  • 1
    @ThomasSablik my instinct told me to use references, but I opted for pointers for no real reason. I know references is better recommended, I will edit. – Everyone Nov 28 '20 at 23:55
2

With C++14 you can use structured bindings, auto return type and tuples. It's pretty similar to Python but a little bit more verbose:

#include <iostream>
#include <tuple>

void display(int n1, int n2, int n3);

auto read(void) {
    int n1, n2, n3;
    
    cout << "Enter three numbers: ";
    cin >> n1;
    cin >> n2;
    cin >> n3;
    return std::make_tuple(n1, n2, n3);
}

int main()
{
    auto [n1, n2, n3] = read();
    display(n1,n2,n3);
    
    return 0;    
}

void display(int n1, int n2, int n3)
{
    std::cout << n1 << " " << n2 << " " << n3 << '\n';
    return;
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62