In the previous lesson, we explained the fundamental types of variables in C++. This lesson will explain the constant and variable scope: formal parameters and local and global variables. You will also learn Variable initiation, definition, and declaration.
You can think of a variable like a container that stores values so our program can use it. Each variable in C++ has a unique type that specifies its memory size and association, the range of values placed inside, and the range of actions performed on the variable.
A variable’s name comes from the fact that you can alter its value. For instance;
int age=45; age 90;
A scope is a section of the program and can define in the below areas:
Local variables have their declarations created inside a function or block. Functions outside of their own are unaware of local variables. The example utilizing local variables is as follows:
Example
// Program add two local variables #include <iostream> using namespace std; int main() { // Local variable declaration: int var1, var2, sum; var1 = 20; var2 = 60; sum = var1 + var2; cout << sum; return 0; }
Output
80
At the beginning of the program, outside of any functions, global variables are defined. The values of the global variables will remain constant throughout your program. Any function may access a global variable. In other words, once a global variable has been declared, you can use it throughout the entire program. The example employing both global and local variables is as follows.
Example
// Program prints local variable #include <iostream> using namespace std; // Global variable declaration int sum = 90; int main() { // Local variable declaration: int sum = 89; //The local variable's value inside a function will always take precedence. cout << sum; return 0; }
Output
89
When function parameters are defined, they are referred to as formal parameters. We will learn about formal parameters in later lessons.
As “constant,” the variable is read-only and unchangeable. Use the term const when you have values that are unlikely to change. We can define variables in C++ that have fixed values. We do that by using the keyword const. Here’s an illustration:
const float PI_VALUE = 3.14; PI_VALUE = 2.58 // Error! PI_VALUE is a constant.
Additionally, you can use the #define preprocessor directive to declare your constant. Here, we’ve declared a constant called PI_VALUE
using the word const. We will encounter an issue if we attempt to alter the value of PI_VALUE.
There are distinctions between the three fundamental concepts of Definition, Declaration, and Initialization of the variable in C++.
A variable is typically introduced when a new memory is allocated to something that we may give a name to.
Example
//declare a variable int myfirstProgram;
A variable declaration tells the compiler that there is a variable with the type and name, so it can continue compiling without needing to know more about the variable. A variable declaration doesn’t do anything until the program is compiled. When the program is linked, the compiler needs to know the variable
type variablelist;
The variable list may have one or more identifier names separated by commas, and the type must be a valid C++ data type, such as char, w char, int, float, double, bool, or any user-defined object, etc. Here are several declarations that are legitimate.
int a, b, c; char e, f; float g, h; double I, j, k, l;
The phrase “int a, b, c;” informs the compiler to create variables with the names “a”, “b”, and “c” that are of the type int.
The user defines the previously declared variable in the declaration.
Example
//Variable declaration int mediumSize; int smallSize; //Variable definition mediumSize=500; smallSize=100;
Simply assigning the value at the moment of declaration is initialization. Variables can be initialized (given a starting value) in their declaration. The equal sign is followed by a constant expression in the initializer, as shown below:
type nameOfVariable = valueAssigned;
Example
// declaration of a, c and b int a, b, c; //definition and initializing of a, b, c a = 10; b = 20; c = 30;
You can clearly understand Definition vs. Declaration vs. Initialization in C/C++ from the table below.
Declaration | Definition | Initialization |
---|---|---|
Simply naming the variable is the declaration. | The definition is an uninitialized declaration. | Initialization is simultaneous definition and declaration. |
Variable values could be erroneous. | The values of variables may or may not be erroneous. | There are no invalid values in variables. |
We can make many declarations. | You can do the definition only once. | You can perform initialization only once, and the system will allocate memory to a variable. |
Memory allocation will not take place during the declaration. | C++ allocates the memory once for the definition, which gives details about that variable. | That variable’s definition and value are given at initialization. |