Global variables are defined outside all function bodies and are available to all parts of the program. - C++ Function

C++ examples for Function:Global Variable

Introduction

Global variables are always available.

If a global variable in one file is declared using the extern keyword in another file, the data is available for use by the second file.

Global.cpp

#include <iostream>
using namespace std;
int globe;
void func();
int main() {
   globe = 12;
   cout << globe << endl;
   func(); // Modifies globe
   cout << globe << endl;
}

Here's a file that accesses globe as an extern. Accessing external global variables extern int globe;

void func() {
   globe = 47;
}

Related Tutorials