3

I have two variables in two different functions, I'd like to store them in the third function without using global variables. How to do it?

something like this

void function1() { 
  a = 1;
  b = 2;
}

void function2() {
  c = 3;
  d = 4;
}

void function3 () {
  cout << a;  
  cout << b;  
  cout << c;  
  cout << d;  
}
2
  • 2
    The best solution depends on what your code actually wants to do. With placeholder names like function1 or a, it's hard to give you an appropriate answer that does not only cover the "what's possible" aspect but also the "what's useful" aspect. Commented Aug 7, 2015 at 18:20
  • 1
    I agree with Christian. You can use a) pass-by-reference, b) return structure or class or tuple or pair c) implement as class member so on... You should choose the one that best suites your context. Commented Aug 7, 2015 at 18:28

4 Answers 4

6

Your functions can return values so you can pass variables to other functions, like so

std::pair<int, int> function1() {
    int a = 1;
    int b = 2;
    return {a, b};
}

std::pair<int, int> function2() {
    int c = 3;
    int d = 4;
    return {c, d};
}

void function3 () {
    int a, b, c, d;
    std::tie(a, b) = function1();
    std::tie(c, d) = function2();
    std::cout << a;  
    std::cout << b;  
    std::cout << c;  
    std::cout << d;  
}

Working demo

Sign up to request clarification or add additional context in comments.

5 Comments

Or std::tuple in place of std::pair to be more generic.
IMHO this is an awkward way to solve a simple problem. It is not at all obvious what std::tie(a, b) = function1(); does.
@ravenspoint .... I can't really help if people are aware of or ignorant of the standard library. If they don't know what a function does, they should look it up. Ignorance is not a reason not to use a tool for its primary purpose. Here's the documentation let the knowledge flow!
Sorry, I disagree. making code unnecessarily complicated in order to show off your knowledge is not good practice. Self explanatory code, without reference to any documentation, is most easily maintained. Remember, debugging is harder than coding - if you write code as complicated as you know how, then you will be unable to debug it.
Point taken. Though I do not think that this solution is particularly difficult nor complicated, but I will leave that up to the OP. If it is understandable, straight-forwared, and solves their problem then they are free to use this solution. Otherwise they may use any other of the suggested methods.
4

Make the functions methods of a class, and the variables attributes of the class.

class A
{
public:
int a;
int b;
int c;
int d;

void function1() { 
  a = 1;
  b = 2;
}

void function2() {
  c = 3;
  d = 4;
}

void function3 () {
  cout << a;  
  cout << b;  
  cout << c;  
  cout << d;  
}
};

3 Comments

While you can do this, I'd highly advise against it. This is pure side effect code.
This is only one step better than using global variables. Unless something actually fits the OO model, you shouldn't default to throwing functions in a class haphazardously.
One step better, exactly! With minimal change to the original code, the original question is answered with straightforward syntax ( i.e no pass by reference or hiding values in pairs ) Why insist on running, when you can walk instead?
4

Use pass-by-reference:

int main() {
    int a;
    int b;
    function1(a, b);

    int c;
    int d;
    function2(c, d);

    function3(a, b, c, d);

    return 0;
}

void function1(int& a, int& b) { 
  a = 1;
  b = 2;
}

void function2(int& c, int& d) {
  c = 3;
  d = 4;
}

void function3(int a, int b, int c, int d) {
  cout << a;  
  cout << b;  
  cout << c;  
  cout << d;  
}

3 Comments

IMHO pass by reference should be avoided. It is hard to see that function1(a, b); actually changes the values of a,b as a side effect.
@ravenspoint The whole point of pass by reference is to show that the function will change the values!
Not so. In the calling code there is no way to see whether it uses pass by value or pass by reference - the syntax is identical. You can only tell by finding the function signature.
2

You can pass them by reference

#include<iostream>
using namespace std;
void function1(int &a,int &b) {
    a = 1;
    b = 2;

}

void function2(int &c, int &d) {
    c = 3;
    d = 4;
}

void function3(int a1,int b1,int c1,int d1) {

    cout << a1;
    cout << b1;
    cout << c1;
    cout << d1;
}
int main(){
    int a = 0, b = 0, c = 0, d = 0;
    function1(a, b);
    function2(c, d);
    function3(a, b, c, d);



}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.