Pass the current state of a function into another function in C/C++

c, c++, calling-convention, function

Solution

Introduce a common struct.

struct State {
    char c;
    int d,e;
};

void funcA(int a, int b){
    State s;
    s.d = 1234; // ...
    // ...
    funcB(s);
}

void funcB(State& s)
{
    //...
}

Problem

Is there a way to pass the current state of a function into another function in C/C++? I mean all the parameters and local variables by current state. For example: ``` void funcA (int a, int b) { char c; int d, e; // Do something with the variables. // ... funcB(); // Do something more. } void funcB() { // funcB() should be able to access variables a,b,c,d & e // and any change in these variables reflect into funcA(). } ``` The code is in bad shape if there is a need for `funcB()` kind of functions. But can it be achieved? This can help if someone is starting to re-factor a long method with multiple parameters.

Original source