Download - FUNCTIONS - II Chapter 9 and 10
![Page 1: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/1.jpg)
1
FUNCTIONS - IIChapter 9 and 10
![Page 2: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/2.jpg)
2
Today we will continue with functions covering….
Passing by Reference Scope
Sorting variables
Function Structure Charts (Ch10)
![Page 3: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/3.jpg)
3
Q: What does x hold after fn call?
void countdown(float n){
while (n>0) {cout <<n; n=n-1;}
cout << "...ZERO!!!“<<endl;}void main( ){ float x=5;
countdown(x);cout<<“after countdown(), x is “<<x;
}
This function changes its parameter
![Page 4: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/4.jpg)
4
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 …
![Page 5: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/5.jpg)
5
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
![Page 6: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/6.jpg)
6
Passing by Reference (contd..)
Then the local variable is only a reference to the argument passed to itThe 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
![Page 7: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/7.jpg)
7
Q: What does x hold after fn call?
void countdown(float& n){
while (n>0) { Added Only 1 Character!
cout <<n; n=n-1;}
cout << "...ZERO!!!“<<endl;}void main( ){ float x=5;
countdown(x);cout<<“after countdown(), x is “<<x;
}
This function uses PBR
![Page 8: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/8.jpg)
8
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
![Page 9: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/9.jpg)
9
Demonstrate politic.cpp in Ch09PBR Functions can “return” more than 1 valueReference 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 lenientIllustrate by modifying politic.cpp
![Page 10: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/10.jpg)
10
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
![Page 11: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/11.jpg)
11
Typical Test Question….output?void f(float x, float& y) { // changes reference argument to 99: x = 88; y = 99; }
void main() { float a = 22, b = 44; cout<< "a = “<< a << ", b = "<< b << endl; f(a,b); cout<< "a = "<< a << ", b = "<< b << endl; }
![Page 12: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/12.jpg)
12
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 (read-only), while b has the new value 99(read-write)
![Page 13: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/13.jpg)
13
Summing up…
![Page 14: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/14.jpg)
14
Your TurnWhat does this program display?
void func1 (int &x, int &y, int z); void 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; }
![Page 15: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/15.jpg)
15
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
![Page 16: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/16.jpg)
16
Today we will continue with functions covering….
Passing by Reference
Scope Sorting variables
Function Structure Charts (Ch10)
![Page 17: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/17.jpg)
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
![Page 18: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/18.jpg)
18
void f(); // f() is global void g(); // g() is global float x = 11; // this x is global void 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()
![Page 19: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/19.jpg)
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
![Page 20: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/20.jpg)
20
PROGRAM OUTPUT for Previous Example
In block inside main(), x = 33
In main(), x = 22
In main(), ::x = 11
In f(), x = 44
In g(), x = 11
![Page 21: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/21.jpg)
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
![Page 22: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/22.jpg)
22
Today we will continue with functions covering….
Passing by Reference
Scope
Sorting variables Function Structure Charts (Ch10)
![Page 23: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/23.jpg)
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)
![Page 24: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/24.jpg)
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;
Used so much we want to make a function
x y temp
3
5
5
5
3
?
3
3
3
![Page 25: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/25.jpg)
25
void swap(float& x, float& y) { // exchanges the values of x , y: float temp = x; x = y; y = temp; }
void main() { float a = 22.2, b = 44.4; cout<<"a = "<<a<<",b = "<<b<<endl; swap(a,b); cout<<"a = "<<a<<",b = "<<b<<endl; }
![Page 26: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/26.jpg)
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
![Page 27: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/27.jpg)
27
Basic Sort
If two adjacent data are out of order, swap
float a, b;
cin>>a>>b;
if (a>b) swap(a, b);
![Page 28: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/28.jpg)
28
Extending Sort for 3 variablescin>>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 smallestif (a>b) swap(a, b);
![Page 29: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/29.jpg)
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
![Page 30: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/30.jpg)
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 stubvoid Sort3(float &a, float &b, float &c){ cout<<“values passed to Sort():”<<endl;
cout<< a <<“ ”<< b <<“ ”<< c << endl;a=1.0;b=2.0;c=3.0;
}
void main( ){ float r, s, t; cin>>r>>s>>t; Sort3(r, s, t); cout<<“in order:” << r <<“ ”<< s <<“ ”<< t << endl;}
This is called a
“function stub”
It just tests
parameter
passing
![Page 31: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/31.jpg)
31
Making a sort3( ) function…revB
// Add swap definition here
//Sort3( ) Sorts three params in numeric order//IN/OUT: a, b, and c are returned in numeric ordervoid 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);
}void main( ){ float r, s, t; cin>>r>>s>>t; Sort3(r, s, t); cout<<“in order:” << r <<“ ”<< s <<“ ”<< t << endl;}
![Page 32: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/32.jpg)
32
Today we will continue with functions covering….
Passing by Reference
Scope
Sorting variables
Function Structure Charts (Ch10)
![Page 33: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/33.jpg)
33
Structure Charts (see p226 +)Show the large scale layout of your program
Each block is a function
Linkage between functions shownvalue and reference parameters
Demonstrate structure charts for Politic.cpp
Circle.cpp
Class.cpp (ch10 folder)
Will help you visualize Project 2 ATM
![Page 34: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/34.jpg)
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
![Page 35: FUNCTIONS - II Chapter 9 and 10](https://reader036.vdocuments.us/reader036/viewer/2022081514/56813082550346895d965dc4/html5/thumbnails/35.jpg)
35
THE END !!!