Download presentation
Presentation is loading. Please wait.
2
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling value returning functions Demo Program: Write a program that calls a function to compute and return sales tax on an item Tuesday Feb 21 Lab Review
3
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
4
3 Scope of Global Variables #include using namespace std; void prettyprint(); int length; void main() { length=24;
5
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; }
6
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;
7
6 Scope of Variables Declared in Main Function prettyprint(); } void prettyprint () { cout << "I cannot refer to length here"<<endl; length=26; }
8
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;
9
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; }
10
9 Lab Exercise for Demo (Due Feb 23) Develop a program using top down design approach that accepts 5 integers from the user and then calls a function to determine and print the odd numbers from this list. Use the explanation as shown below for determining odd numbers: –if you divide the number by 2 and the remainder is zero, it means that the dividend was an even number 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.