|
|
|
|
|
|
Textbook |
|
Chapter 6 : Iteration |
|
6.2.1: for loops |
|
Chapter 5 : Functions |
|
5.5: Parameters |
|
5.8: Reference Parameters |
|
Package 1 – Patterns |
|
NameUseFunctions |
|
LoopCounting |
|
Package 2 – Recitation 3 |
|
Package 3 – Lab 3 |
|
|
|
|
|
|
void Picture(int x, int y, int size); |
|
void Picture(int x, int y, int size) { |
|
PaintRect(x, y, x + size, y + 0.6*size); |
|
… |
|
} |
|
Paint a square picture of a given size with the
left top corner at coordinates (x,y) |
|
Each graphic function (except SetForeColor)
should in term of x, y and size |
|
Don’t put fixed number, such as PaintRect(100,
100, 150, 150) |
|
|
|
|
|
|
void BigPicture(); |
|
void UserPicture(); |
|
void FourPictures(); |
|
|
|
// write a function BigPicture that first clear
the GW |
|
// then display the picture to fill the entire
GW |
|
void BigPicture() { |
|
ClearDrawing(); |
|
Picture(0, 0, 300); |
|
} |
|
|
|
|
// write a function UserPicture that first clear
the GW |
|
// then ask user to enter the desired location
and size of the picture |
|
// Use a default location(100,100) and default
size 150 |
|
Void UserPicture() { |
|
ClearDrawing(); |
|
int x = RequestInt(“Enter x (0 ~ 300):
“, 100); |
|
int y = RequestInt(“Enter y (0 ~ 300):
“, 100); |
|
int size = RequestInt(“Enter size (0 ~
300): “, 150); |
|
Picture(x, y, size); |
|
} |
|
|
|
|
|
|
// write a function FourPictures that first
clear the GW |
|
// then display four pictures of size 150 so
that they fill the window |
|
Void UserPicture() { |
|
ClearDrawing(); |
|
Picture(0, 0, 150); |
|
Picture(?, ?, 150); |
|
Picture(?, ?, 150); |
|
Picture(?, ?, 150); |
|
} |
|
|
|
|
|
|
|
Question 23 will use pass parameters to function
by reference |
|
About MakeChange |
|
Similar to Q22 in recitation 1 |
|
You may wait |
|
Tuesday’s lecture will finish all about function |
|
|
|
|
|
Functions |
|
Function declaration: signature |
|
Function implementation |
|
Function call |
|
Flow of control |
|
if statement |
|
C++ source file layout |
|
C++ programming style |
|
|
|
|
|
|
|
Functions |
|
Parameter passed by value |
|
Parameter passed by reference |
|
Increment operator ++ |
|
Decrement operator -- |
|
|
|
|
void Swap(int x, int y); // function
declaration |
|
void Swap(int x, int y) { // function
implementation |
|
int temp; |
|
cout << “
x = “ << x << “ y = “ << y << endl; // x=5 y =9 |
|
temp = x; |
|
x = y; |
|
y
= temp; |
|
cout << “ x = “ << x << “ y = “
<< y << endl; // x=9 y =5 |
|
} |
|
int main() { |
|
int m = 5, n = 9; |
|
Swap(m, n); // function call |
|
cout << “ n = “ << m << “ n = “
<< n << endl; // m=5 n =9 |
|
return 0; |
|
} |
|
|
|
|
|
|
|
When main function calls Swap functions |
|
First makes copies of actual arguments m and n |
|
Use copies to initialize formal argument x and y |
|
Any changes made for x and y in Swap have
nothing to do with actual argument m and n |
|
Because only values of m and n passed into
function Swap |
|
|
|
|
void Swap(int& x, int& y); //
function declaration |
|
void Swap(int& x, int& y) { //
function implementation |
|
int temp; |
|
cout << “
x = “ << x << “ y = “ << y << endl; // x=5 y =9 |
|
temp = x; |
|
x = y; |
|
y
= temp; |
|
cout << “ x = “ << x << “ y = “
<< y << endl; // x=9 y =5 |
|
} |
|
int main() { |
|
int m = 5, n = 9; |
|
Swap(m, n); // function call |
|
cout << “ n = “ << m << “ n = “
<< n << endl; // m=9 y =5 |
|
return 0; |
|
} |
|
|
|
|
|
|
|
C++ syntax |
|
data-type& reference-name |
|
Example |
|
float& num1 |
|
Declare that num1 is a reference parameters that
will be used to store the address of a float |
|
|
|
|
|
In C++, & means “the address of” |
|
When an & symbol is used within a function
declaration, it refers to “the address of” the preceding data type. |
|
Declaration such as float& num1 is sometimes
more clearly understood if it is read backwards. |
|
“num1 is the address of a floating-point value” |
|
|
|
|
Here actual arguments m,n and formal arguments x,
y reference the same data items. |
|
The significance of this is that the values in
the actual arguments (m, n) can now be altered from within Swap by using
the formal names (x, y). |
|
x, y do not store copies of the values in m, n,
but directly access the locations in memory set aside for these two
arguments. |
|
|
|
|
|
|
Question: write a function that increments an
integer. |
|
Method 1: The user supplies the integer value
(that does not need to be stored as an integer variable, but can be
computed as the function call is made), by using the value argument. The
function returns the incremented value as the function return value. |
|
|
|
|
|
int Increment(int n); // function
declaration |
|
int Increment(int n){ // function
definition |
|
return (n+1); |
|
} |
|
int main() { |
|
int x = 25; |
|
x = Increment(x); |
|
int y = Increment(x); |
|
cout << x << " -- "
<< y << " -- " << Increment(y) |
|
<< Increment(6)
<< endl; |
|
return 0; |
|
} |
|
// this will print 26 -- 27 -- 28 -- 7 |
|
|
|
|
|
|
|
|
|
|
|
Question: write a function that increments an
integer. |
|
Method 2 : the user supplies a name of the
variable whose value should be incremented. The function code will be able
to read the value of this variable and change it. We say that the argument
is passed by reference and indicate this in the signature by following the
data type with an &. |
|
|
|
|
|
|
void Increment(int& n); // function
declaration |
|
void Increment(int& n){ // function
definition |
|
n = n+1; |
|
}; |
|
int main() { |
|
int x = 25; |
|
cout << x << " ~~~
"; |
|
Increment(x); |
|
cout << x << endl; |
|
return 0; |
|
} |
|
// this will print 25 ~~~ 26 |
|
|
|
|
|
|
|
|
void Increment(int& n); // function
declaration |
|
void
Increment(int& n){ // function definition |
|
n = n+1; |
|
}; |
|
int
main() { |
|
int x = 25; |
|
cout << x << " ~~~
" << Increment(x) << Increment(6) << endl; |
|
return 0; |
|
} |
|
Increment(x) : the function call does not result
in any usable data value and so cannot be used in any computation or in the
output stream. |
|
Increment(6) : cannot convert parameter 1 from
'const int' to 'int &' |
|
|
|
|
|
Functions |
|
Parameter passed by value |
|
Parameter passed by reference |
|
Increment operator ++ |
|
Decrement operator -- |
|
|
|
|
variable = variable + 1 can be replaced by the
expression variable++ or ++variable |
|
Examples |
|
|
|
|
|
Prefix increment operator : when ++ appears
before a variable |
|
Example : ++x; |
|
Postfix increment operator : When ++ appears
after a variable |
|
Example : x++; |
|
|
|
|
|
++x |
|
increment x |
|
then use new value |
|
x++ |
|
use old value of x |
|
then increment x |
|
|
|
|
|
The distinction between a prefix and postfix
increment operator is important when the variable being incremented is used
in an assignment expression. |
|
|
|
k = ++n; |
|
is equivalent to : n = n + 1;
// increment n first |
|
k = n;
// assign n’s value to k |
|
k = ++n; |
|
is equivalent to : k = n;
// assign n’s value to k first |
|
n = n + 1;
// and then increment n |
|
|
|
|
|
|
int n = 5; |
|
int k = 7; |
|
k = ++n; |
|
cout << “ k = “ << k << “
n = “ << n << endl; |
|
// will print : k = 6 n = 6 |
|
|
|
int n = 5; |
|
int k = 7; |
|
k = n++; |
|
cout << “ k = “ << k << “
n = “ << n << endl; |
|
// will print : k = 5 n = 6 |
|
|
|
|
|
|
variable = variable - 1 can be replaced by the
expression variable-- or --variable |
|
Examples |
|
|
|
|
int n = 3; |
|
int k = --n; |
|
cout << “ k = “ << k << “
n = “ << n << endl; |
|
// will print : k = 2 n = 2 |
|
|
|
int n = 3; |
|
k = n--; |
|
cout << “ k = “ << k << “
n = “ << n << endl; |
|
// will print : k = 3 n =2 |
|
|
|
|
|
|
|
Four possibilities |
|
|
|
|
|
|
|
|
|
Auto increment/decrement most useful with
subscripts, pointers and loop counters |
|
May be used in expressions |
|
Often used in stand-alone |
|
Z = I * I; |
|
++I; |
|
|
|
|
|
Functions |
|
Parameter passed by value |
|
Parameter passed by reference |
|
Increment operator ++ |
|
Decrement operator -- |
|
|
|
|
|
More on function |
|
Pass parameters by reference |
|
Flow of control |
|
Repetition – for statement |
|
Format output |
|
|
|
|
|
|
|