Data Sharing [C++] [01]: Between Functions Within A File

3 minute read

Published:

Codes in this post can be found in file DataSharingBetweenFunctions.cpp.


global variable

The simplest way to share data between different functions within a file is to simply define global variables on top of the program.

For example, when a global variable globalVariable and two functions funtion1 and function2 are defined and called in the main function, the results would be 1 and 0 respectively.

int globalVariable = 0;

void function1() {
    globalVariable += 1;
}

void function2() {
    globalVariable -= 1;
}
int main() {
    function1();
    cout << globalVariable << endl;
    function2();
    cout << globalVariable << endl;
    system("pause > 0");
}

However, there is a problem here. When a local variable with the same name as the global variable is defined, the global variable will be overridden by the local variable. For example, if the globalVariable is defined again in the main function, the resulta would become 2 and 2 respectively.

int main () {
    int globalVariable = 2;
    function1();
    cout << globalVariable << endl;
    function2();
    cout << globalVariable << endl;
    system("pause > 0");
}

To solve this problem, two simple methods are available. We can either define a namespace to include the global variable, or we can shadow the local variable using extern declaration.


namespace

Here a namespace named g is defined, again, the results would be 1 and 0 respectively.

namespace g{
    int globalVariable = 0;
}

void function1() {
    g::globalVariable += 1;
}

void function2() {
    g::globalVariable -= 1;
}
int main() {
    int globalVariable = 2;
    function1();
    cout << g::globalVariable << endl;
    function2();
    cout << g::globalVariable << endl;
    system("pause > 0");
}

extern

Here the extern declaration is used before calling funtion1 and function2, again, the results would be 1 and 0 respectively. ({ and } are necessary, which define the scope of the variable.)

int globalVariable = 0;

void function1() {
    globalVariable += 1;
}

void function2() {
    globalVariable -= 1;
}
int main() {
    int globalVariable = 2;	
    {
        extern int globalVariable;
        function1();
        cout << globalVariable << endl; 	
        function2();
        cout << globalVariable << endl;
    }
    return 0;
}

static

static can be very useful when we have to define a variable within a while loop and don’t want to create and destroy it each time it comes into and goes out of scope, so that we can maintain its value between function calls. Here is a simple example, using stepCount, it’s easy to control which part of athe code to execute. Again, the results would be 1 and 0 respectively.

int main() {
    while(1) {
        static int stepCount = 0; 
        if (stepCount == 0){
            function1();
            cout << globalVariable << endl;
            stepCount++;
        }else if (stepCount = 1){
            function2();
            cout << globalVariable << endl;
            stepCount++;
        }else{
            break;
        }
   }  
   return 0;
}

Another usage of static is to define the static variable within a function. The variable is initialized before the first time execution, it is not destroyed when a function ends, instead it is destroyed when the program terminates. Below is a simple example, the results would be 0 and 1 respectively.

int function() {
    static int stepCount = 0;
    return stepCount++;
}
int main(){
    cout << function() << endl;
    cout << function() << endl;
}


Table of Contents

Comments