Introduction to User-Defined Function and its Requirements

Local and Global Variables

A variable that is defined inside a function (defined inside the body of the function between the braces) is known as the local variable or the automatic variable. However, its scope is limited only to the function in which it is definable. Moreover, in simple language, a local variable exists and we can easily access it only inside a function. This article will help you learn about local and global variables in detail.

local and global variables

The life of a local variable comes to an end (it gets destroyed) when the function exits. Let us know more about local and global variables through this.

Example 1st

Local Variable

                    

#include 

using namespace std;

void test();

int main()

{

// local variable to main()

int var = 5;

test();

// illegal: var1 not declared inside main()

var1 = 9;

}

void test()

{

// local variable to test()

int var1;

var1 = 6;

// illegal: var not declared inside test()

cout << var;

}

 

We cannot use the variable ‘var’ inside test() and we cannot use the var1 inside the main() function.

Keyword auto was also applied to define the local variables before as: auto int var;

But, after C++ 11 auto has a different meaning and should not be used to define the local variables.

Browse more Topics under User-Defined Function and its Requirements

Global Variable

If a variable is stated outside all the functions, then we call it a global variable.

The scope of a global variable is basically the complete program. Moreover, this means that we can use it and change it at any portion of the program after its declaration.

Likewise, its life ends only when the program finishes.

Example 2nd

Global variable

                    

#include <iostream>

using namespace std;

// Global variable declaration

int c = 12;

void test();

int main()

{

++c;

// Outputs 13

cout << c <<endl;

test();

return 0;

}

void test()

{

++c;

// Outputs 14

cout << c;

}

Output

In the program given above, ‘c’ is a global variable.

This variable is visible to the main() function as well as the test() function in the program given above.

Static Local Variable

We use the keyword static to specify a static variable, for example:

… .. …

int main()

{

static float a;

… .. …

}

A static local variable is present only inside a function in which it is declared (similar to a local variable) but its lifetime begins when the function is called and finishes only when the program finishes.

The main dissimilarity between the local variable and the static variable is that the value of the static variable persists in the conclusion of the program.

Example 3rd

Static local variable

                    

#include <iostream>

using namespace std;

void test()

{

// var is a static variable

static int var = 0;

++var;

cout << var << endl;

}

int main()

{

test();

test();

return 0;

}

Output

FAQs on Local and Global Variables

Question 1: Describe, how can we make a local variable global in the C++ language?

Answer: We can declare the global variables that are, the non-local variables by the declaration of them outside of any function definition. Moreover, it is generally best to put all the global declarations near the starting of the program, before the 1st function. However, we can recognize a variable only from the point it is declared, to the conclusion of the file.

Question 2: What are the local variables in the C++ language?

Answer: In computer science, a local variable is basically a variable that is having a local scope. Moreover, the local variable references in the function or block where it is declared override the same variable name in the bigger scope. However, the local variables may comprise a lexical or a dynamic scope.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.