Download presentation
Presentation is loading. Please wait.
1
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo
2
2 Scope of Variables If a variable is declared WITHIN a function, it is only visible and active when that function is active. It is known as a local variable. If a variable is declared on top, OUTSIDE and BEFORE all the functions, it is visible and active throughout the full life of the program and it is known as a global variable
3
3 Scope of Global Variables #include using namespace std; void prettyprint(); int length; void main() { length=24;
4
4 Scope of Global Variables cout << "I can refer to length here" << length<<endl; prettyprint(); } void prettyprint () {length=26; cout << "I can also refer to length here"<<length<<endl; }
5
5 Scope of Variables Declared in Main Function #include using namespace std; void prettyprint(); void main() { int length=24; cout << "I can refer to length here" << length<<endl;
6
6 Scope of Variables Declared in Main Function prettyprint(); } void prettyprint () { cout << "I cannot refer to length here"<<endl; length=26; }
7
7 Scope of Variables Declared in Any Function Except Main #include using namespace std; void prettyprint(); void main() { cout << "I cannot refer to length here" <<endl; length=24;
8
8 Scope of Variables Declared in Any Function Except Main prettyprint(); } void prettyprint () { int length; cout << "I can only refer to length here"<<length<<endl; length=26; }
9
9 Lab Exercise for Demo (Due Sep 28) Develop a program using top down design approach that accepts 5 values from the user and then calls a function to determine and print the odd numbers from this list. Use the if clause as shown below for determining odd numbers: Make sure that all the variables are declared as global variables and you do not pass any variables to the function
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.