FUNCTIONS - II Chapter 9 and 10 1 Today we will continue with functions covering…. Passing by Reference Scope Sorting variables Function Structure Charts (Ch10) 2 This function changes its parameter void countdown(float n) { while (n>0) { cout <<n; n=n-1;} cout << "...ZERO!!!“<<endl; } int main( ) { float x=5; countdown(x); cout<<“after countdown(), x is “<<x; } Q: What does x hold after fn call? 3 Program Output 54321…ZERO!!! after countdown(), x is 5 (WHY?) Pass-by-Value: Only a copy of the argument x is given to the function Even though n changes, x retains original value Sometimes you need the function to change the argument. Thus we also have … 4 Passing by Reference This is done in situations where a function needs to change the value of the argument passed to it To pass an argument by reference, simply append an ampersand ‘&’, to the type specifier in the function’s parameter list 5 Passing by Reference (contd..) Then the local variable is only a reference to the argument passed to it The argument is read-write instead of read-only(as in the case of pass by value) Any change to the local variable inside the function will cause the same change to the argument that was passed to it 6 This function uses PBR void countdown(float& n) { while (n>0) { Added Only 1 Character! cout <<n; n=n-1;} cout << "...ZERO!!!“<<endl; } int main( ) { float x=5; countdown(x); cout<<“after countdown(), x is “<<x; } Q: What does x hold after fn call? 7 New Program Output 54321…ZERO!!! after countdown(), x is 0 (WHY?) Pass-by-Reference: Direct access to argument x is given to the function (Read/Write) When it changes “n”, it really changes x Here n is another name for the argument, x Often called a reference or “alias” to x 8 Demonstrate politic.cpp in Ch09 PBR Functions can “return” more than 1 value Reference parameters can be same name or different name than arguments Draw lines between arguments and parameters Arguments to Reference parameters must be variables of the same type (no constants) Arguments to Value parameters can be constants, variables, or expressions, and type is more lenient Illustrate by modifying politic.cpp 9 Demonstrate circle.cpp, ch09 PBR Functions can calculate 2 or more quantities and return them Some parameters are Value, some are Reference Draw lines between arguments and parameters 10 Typical Test Question….output? void f(float x, float& y) { // changes reference argument to 99: x = 88; y = 99; } int main() { float a = 22, b = 44; cout<< "a = “<< a << ", b = "<< b << endl; f(a,b); cout<< "a = "<< a << ", b = "<< b << endl; } 11 The call f(a,b) passes a by value to x and it passes b by reference to y x is a local variable that is assigned a’s value of 22 y is an alias for the variable b whose value is 44 The function assigns 88 to x but that has no effect on a When it assigns 99 to y it is really assigning 99 to b, because y is an alias for b Finally, a still has its original value 22 (readonly), while b has the new value 99(read-write) 12 Summing up… 13 Your Turn What does this program display? void func1 (int &x, int &y, int z); int main() { int a=2, b=2, c=2; func1 ( a, b, c); cout <<"a="<<a<<"b="<<b<<"c="<< c <<endl; } void func1 (int &x, int &y, int z) { x=x+1; y=y-1; z=z+2; } 14 How do you know when to use &? Don’t overdo it because you are not sure Ask…will the function need to initialize or update an argument you are giving it? YES…then use a reference parameter NO…then use a value parameter See circle.cpp again to study the value and reference parameters 15 Time for Break !! Download Lab8Functions.cpp Do problems 1-4 16 Today we will continue with functions covering…. Passing by Reference Scope Sorting variables Function Structure Charts (Ch10) 17 Remember scope ? The scope of a name consists of that part of the program where it can be used It begins where the name is declared The scope extends to the end of the innermost block that contains the declaration 18 void f(); // f() is global void g(); // g() is global float x = 11; // this x is global int main() { float x = 22; { float x = 33; cout<<"In block inside main(), x = "<<x<<endl; } // end scope of internal block cout<<"In main(), x = "<<x<< endl; cout<<"In main(), ::x = "<<::x<<endl; //global x f(); g(); } // end scope of main() void f() { float x = 44; cout << "In f(), x = " << x << endl; } // end scope of f() void g() { cout << "In g(), x = " << x << endl; } // end scope of g() 19 In the previous example… f() and g() and the first x are global – their scope includes the entire file The second x is declared inside main() , it is accessible only from within the main() The third x is declared inside an internal block, so its scope is restricted to that internal block 20 PROGRAM OUTPUT for Previous Example In In In In In block inside main(), x = 33 main(), x = 22 main(), ::x = 11 f(), x = 44 g(), x = 11 21 Scope and You: the Rules!! Don’t use global variables for now. You don’t need them. Lazy programmers try to skip writing parameter lists by making all function variables global—0 points if you do!! Best is to declare all variables needed at beginning of main/other function Advice—use different names for function call arguments and function parameters 22 Today we will continue with functions covering…. Passing by Reference Scope Sorting variables Function Structure Charts (Ch10) 23 Sorting Data Arranging data in increasing order A real “bread and butter” CS/IS task Sorted data are easier to search Usually a slow process for long lists of data Efficiency of advanced sort methods can result in tremendous gains in processing time (CSIS 10B) 24 Sort works by using Swap One of the most useful algorithms Swap two variables: float x=3, y=5, temp; temp=x; x=y; y=temp; x y temp 3 5 5 5 3 ? 3 3 3 Used so much we want to make a function 25 void swap(float& x, float& y) { // exchanges the values of x , y: float temp = x; x = y; y = temp; } int main() { float a = 22.2, b = 44.4; cout<<"a = "<<a<<",b = "<<b<<endl; swap(a,b); cout<<"a = "<<a<<",b = "<<b<<endl; } 26 Let’s understand that program.. The formal parameters x and y are declared as reference variables : float& x, float& y The reference operator & makes x and y synonyms for the arguments passed to the function 27 Basic Sort If two adjacent data are out of order, swap float a, b; cin>>a>>b; if (a>b) swap(a, b); 28 Extending Sort for 3 variables cin>>a>>b>>c; If any two adjacent data are out of order, swap if (a>b) swap(a, b); if (b>c) swap(b, c); Recheck 1st two in case c was smallest if (a>b) swap(a, b); 29 Demonstrate alpha3.cpp, ch09 Modify to simplify, only swap w1 and w2 and show changes What if you want to sort integers? Function overloading… make another swap(int &x, int &y) Compiler can tell which one to go to during execution by looking at the arguments you are using 30 Making a Sort3( ) function…revA //Sort3( ) Sorts three params in numeric order //IN/OUT: a, b, and c are returned in numeric order // currently only a stub void Sort3(float &a, float &b, float &c) { cout<<“values passed to Sort():”<<endl; This is called a cout<< a <<“ ”<< b <<“ ”<< c << endl; “function stub” a=1.0; b=2.0; It just tests c=3.0; parameter } passing int main( ) { float r, s, t; cin>>r>>s>>t; Sort3(r, s, t); cout<<“in order:” << r <<“ ”<< s <<“ ”<< t << endl; } 31 // Add swap definition here //Sort3( ) Sorts three params in numeric order //IN/OUT: a, b, and c are returned in numeric order void Sort3(float &a, float &b, float &c) { if (a>b) swap(a, b); if (b>c) swap(b, c); if (a>b) swap(a, b); } int main( ) { float r, s, t; cin>>r>>s>>t; Sort3(r, s, t); cout<<“in order:” << r <<“ ”<< s <<“ ”<< t << endl; } Making a sort3( ) function…revB 32 Today we will continue with functions covering…. Passing by Reference Scope Sorting variables Function Structure Charts (Ch10) 33 Structure Charts (see p226 +) Show the large scale layout of your program Each block is a function Linkage between functions shown value and reference parameters Demonstrate structure charts for Politic.cpp Circle.cpp Class.cpp (ch10 folder) Will help you visualize Project 2 ATM 34 Demonstrate class.cpp Notice the documentation of in and out parameters Also notice the passing of a filestream to find_mark function ofstream& fout This will be important later in Project 2 35 THE END !!! 36