Lecture 10
Local and Global Variables Nearly every programming language has a concept of local variable. As long as two functions mind their own data, as it were, they won t interfere with each other. Ex. i in prime function and i in main function are not same! Why global variables? Global variables are often the best way to communicate information between functions.
Global Variables #include <iostream> #include <cmath> using namespace std; int status = 0; // global variable status void main () { //
prime again Use a global variable divisor to return divisor if n is not a prime. int divisor =0; // global a variable bool prime(int n){ if (n<0) n=-n; // to handle negative number. if (n==1) {divisor=1; return false; //extra code for 1. for(int i=2;i<=sqrt(n);i++){ if(n%i==0) {divisor=i; return false; return true;
prime again #include <iostream> #include <cmath> using namespace std; int divisor =0; //global variable bool prime(int n); //declare. int main() { int i; while (true) { cout << "Enter num (0 = exit) and press ENTER: ; cin >> i; if (i == 0) break; if (prime(i)) cout << i << " is prime" << endl; else cout << i << " is not prime since " <<i<<"="<<divisor<<"*"<<i/divisor<< endl; return 0;
Declaring variables in a block Where we declare a variable in our C++ program, inherently determine where this variable would be accessible. A block of code is defined as code inside { If statements, while loop, for loop, are usually follow by a block of code. If the variable is declared in the block, it can t be accessed anywhere else.
Example of block declaration while(c<10){ for( int i =0; i<10; i++) cout<< i; cout<< i; int i = 0 cout<< i; c++ cout << i;
Declaring variables in a function A variable defined in a function is contained with in the function block of code. This means that you can not share variables between functions. Each function needs to be self sufficient in terms of declaring variables that it needs. OR The variable must be pass into the function as a parameter.
Passing variables as arguments When we pass variable as an argument from main function to a sub function Only the value of the variable is passed through That is why we can give the variable a different name That is also why whatever changes we do to this new, different variable wouldn t affect main program.
Example of Passing variables by value #include <iostream> using namespace std; double avg(double, double); int main() { double a = 1.2; double b = 2.7; cout << "Avg is " << avg(a,b); double avg(double x, double y){ double v = (x + y)/2; return v; #include <iostream> using namespace std; void print_results(double a, double b); double avg(double x, double y); int main() { double a = 1.2, b=2.7; print_results(a, b); return 0; void print_results(double a, double b) { cout << "Average is: " << avg(a, b) << endl; double avg(double x, double y) { double v = (x + y)/2; return v;
Pass by value Everything we have done so far is pass by value The value with in the variable is pass to the sub function The sub-function will take the value and store in a variable within its own scope The variable with the value is discarded when it runs out of scope(in the sub-function)
How can we keep the change? One way is to return the newly calculated value to the calling function Then assign the value to the same variable, effectively updating it However there is another way to do it in c++
Passing variables by reference Instead of giving a copy of the value to the sub function, we can give the reference of the value. When we give the reference of the value, the sub function would be able to change the value. Reference is another name referring to the location where the value is stored.
Example Passing variables by value #include <iostream> using namespace std; void update(int); int main() { int n = 100; cout<< Value of n:"<< n <<endl; update(n); cout<< Value of new n: "<< n<<endl; return 0; void update(int n) { cout<< IN UPDATE:Value of n:"<<n<<endl; n =0; cout<< IN UPDATE:Value of n again:"<<n<<endl; Passing by reference #include <iostream> using namespace std; void update(int&) int main() { int n =100; cout<< Value of n:"<<n<<endl; update(n); cout<< Value of new n:"<<n<<endl; return 0; void update(int &n) { cout<< IN UPDATE:Value of n:"<<n <<endl; n= 0; cout<< IN UPDATE:Value of n again:"<<n<<endl;
Other Uses Passing by reference would also come in handy if you need to read in a set of inputs from the user and you don t want to write a function to read in one at a time. Example void getthreevalue(int &a,int &b,int &c) { cout<< "Enter a, b, c: "; cin >>c >> b>> c;
Exercise What is the output of this program? Answer:??? #include <iostream> void sss(int,int &,int &); void mmm(int,int &,int &); using namespace std; int main () { int x = 2, y = 3, z = 3; sss (x, y, z); cout << x << " " << y << " " << z<<endl; sss (y, z, x); //note the order of the variables cout << x << " " << y << " " << z<<endl; mmm (y, z, x); //note the order of the variables cout << x << " " << y << " " << z << endl; return 0; void sss (int a, int &e, int &c) { a = e + 1; c = 4 + a; e = 9; void mmm (int e, int &c, int &a) { e = 11; a = e;
Exercise Exercise 2: Write a function named print_out that prints all the whole numbers from 1 to N. Test the function by placing it in a program that passes a number n to print_out, where this number is entered from the keyboard. The print_out function should have type void; it does not return a value. The function can be called with a simple statement: print_out(n); Exercise 3: Write a function do primetest. Exercise 4: Optimize the prime-number function by calculating the square root of n only once during each function call. Declare a local variable sqrt_of_n of type double. (Hint: A variable is local if it is declared inside the function.) Then use this variable in the loop condition.
Homework 7 Due one week from today Question 1: Ask user enter an integer, and print all the prime numbers less than user input integer. User enter integer: 10 prime number less than 10: 2, 3, 5, 7 Question 2: Ask user enter an integer n, and print 1 1, 2 2,3 3..n n. (use long int here, and for loop, do not use pow() function) Question 3: Use nested for loops to write a n x n multiplication table. User enter integer: 5 1 2 3 4 5 1 1 2 3 4 5 2 2 4 6 8 10 3 3 6 9 12 15 4 4 8 12 16 20 5 5 10 15 20 25