Global and Local Variables in C Plus Plus
68This tutorial illustrates basic concepts for creating and manipulating local and global variables in C++ (C Plus Plus).
The source code, listed below, includes a main() function, two other functions, and two headers for the functions. Variables are declared in different locations in the modules. Global variables are declared outside of the functions in the module, usually ((almost always) at the top of the module immediately above the first function in the module.
In C Plus Plus, a global variable is any variable declared outside of a class and outside of a code block. A local variable is any variable declared inside a code block. Local variables are visible only inside the code block. Global variables are visible to all modules in the project.
Local variables, by definition, are declared inside a block of code. For the purposes of this tutorial, the only block of code we are interested in is the block that delimits the entire function. Other blocks include loop constructs and decision constructs, but those are not addressed here.
This tutorial does not address namespace issues or class issues.
/******************************************************************************
* Scope and Lifetime of Variables main.cpp *
* *
* Author: nicomp *
* *
* *
* Abstract: This project illustrates techniques for creating and using *
* local and global symbols *
* *
******************************************************************************/
#include <iostream>
#include "Function01.h"
#include "Function02.h"
using namespace std;
// Global variables go here. Outside of all functions.
// We can use global variables to share info between modules, but it is not good code.
int intGlobalIntDeclaredInFunctionMain;
void main()
{
// Local variables are declared here
// They can be declared inside any code block: {}.
int intLocalToMain;
Function01();
Function02();
// All of the globals are visible here
intGlobalIntDeclaredInFunction01Module = 42;
intGlobalIntDeclaredInFunction02Module = 42;
intGlobalIntDeclaredInFunctionMain = 42;
}
/******************************************************************************
* Scope and Lifetime of Variables Function01.cpp *
* *
* Author: nicomp *
* *
* *
* *
* Abstract: This project illustrates techniques for creating and using *
* local and global symbols *
* *
******************************************************************************/
#include <iostream>
using namespace std;
// Global variables go here. Outside of all functions.
int intGlobalIntDeclaredInFunction01Module;
void Function01()
{
// Local variables are declared here
// They can be declared inside any code block: {}.
int intLocalToFunction01;
// All of the globals are visible here
intGlobalIntDeclaredInFunction01Module = 42;
intGlobalIntDeclaredInFunction02Module = 42;
intGlobalIntDeclaredInFunctionMain = 42;
}/******************************************************************************
* Scope and Lifetime of Variables Function02.cpp *
* *
* Author: nicomp *
* *
* *
* *
* Abstract: This project illustrates techniques for creating and using *
* local and global symbols *
* *
******************************************************************************/
#include <iostream>
using namespace std;
// Global variables go here. Outside of all functions.
int intGlobalIntDeclaredInFunction02Module;
void Function02()
{
// Local variables are declared here
// They can be declared inside any code block: {}.
int intLocalToFunction02;
// All of the globals are visible here
intGlobalIntDeclaredInFunction01Module = 42;
intGlobalIntDeclaredInFunction02Module = 42;
intGlobalIntDeclaredInFunctionMain = 42;
}/****************************************************************************** * Scope and Lifetime of Variables Function01.h * * * * Author: nicomp * * * * * * * * Abstract: This project illustrates techniques for creating and using * * local and global symbols * * * ******************************************************************************/ #include <iostream> using namespace std; // Prototype for Function01 void Function01();
/****************************************************************************** * Scope and Lifetime of Variables Function02.h * * * * Author: nicomp * * * * * * * * Abstract: This project illustrates techniques for creating and using * * local and global symbols * * * ******************************************************************************/ #include <iostream> using namespace std; // Prototype for Function02 void Function02();
PrintShare it! — Rate it: up down flag this hub



