Upcoming SlideShare
×

# Scope of variables

283 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
283
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
6
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Scope of variables

1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
2. 2. Scope Of Variables Sibin Raj P.R sibinrajotk@gmail.com 8086498752
3. 3. WHAT IS A VARIABLE? • A variable is the name for a place in the computer’s memory where some data are stored • Every varibale has a name called variable name and a data type E.g. int a int- data type a- variable name
4. 4. Local Variables • Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code
5. 5. Global variables • A global variable is a variable which is accessible in multiple scopes. It is important to note that global variables are only accessible after they have been declared. • Global ariables that are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the lifetime of your program. • A global variable can be accessed by any function. • i.e. a global variable is available for use throughout your entire program after its declaration.
6. 6. • // Difference between Global and Local Variables #include <iostream> namespace std; int result; // Global variable declaration int main () { int a, b; // Local variable declaration a = 10 , b = 20 ; // initialization result = a + b; cout << result; // Print result return 0; }
7. 7. SCOPE • The scope of a variable is the block of code where the variable is valid for use • If declaration is made outside the bodies of all functions and outside the main program is called global variable • A local declaration is one that is made inside the body of a function • It is possible to declare the same identifier name in different parts of the program
8. 8. • Undisciplined use of global variables may lead to confusion and debugging difficulties • Instead of using global variables in functions, try passing local variables by reference • The use of global variable is not encouraged due to the lack of protection of data