Notes
Outline
COM1100
Fundamentals of Computer Science –Winter 2000
Lecture 7
01/24/99
Reading material this week
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
Picture lab – Picture()
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)
Picture lab – BigPicture()
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);
}
Picture lab – UserPicture()
// 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);
}
Picture lab – FourPictures()
// 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);
}
Recitation 2
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
Previous lectures
Functions
Function declaration: signature
Function implementation
Function call
Flow of control
if statement
C++ source file layout
C++ programming style
Today’s lecture
Functions
Parameter passed by value
Parameter passed by reference
Increment operator ++
Decrement operator --
Functions 
-- parameter passed by value
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;
}
Actual and Formal arguments – pass by value
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
Functions 
-- parameter passed by reference
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;
}
Reference parameter
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
& symbol
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”
Actual and Formal arguments – pass by reference
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.
The equivalence of actual and formal arguments when passed by reference
Method 2 – pass by value
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.
Method 1 -- Example
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
Method 2 – pass by reference
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 &.
Method 2 -- Example
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
How many mistakes ?
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 &'
Today’s lecture
Functions
Parameter passed by value
Parameter passed by reference
Increment operator ++
Decrement operator --
Increment operator ++
variable = variable + 1 can be replaced by the expression variable++ or ++variable
Examples
Prefix vs. Postfix
increment operator
Prefix increment operator : when ++ appears before a variable
Example : ++x;
Postfix increment operator : When ++ appears after a variable
Example : x++;
Difference between ++x and x++
++x
increment x
then use new value
x++
use old value of x
then increment x
"The distinction between a prefix..."
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
Examples-- interview questions from Microsoft
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
Decrement operator --
variable = variable - 1 can be replaced by the expression variable-- or --variable
Examples
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
Summary -- increment and decrement operators
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;
Today’s lecture
Functions
Parameter passed by value
Parameter passed by reference
Increment operator ++
Decrement operator --
Lecture Tuesday (01/25)
More on function
Pass parameters by reference
Flow of control
Repetition – for statement
Format output