PIC10A Lecture 10

Reading

p134-159

call by value

double finalScore(double midterm, double exam) {
double result1 = 0.5*midterm + 0.5*exam;
double result2 = 0.4*midterm + 0.6*exam;
// the final score is the max of result1 and result2
if(result1 >= result2)
return result1;
else
return result2;
}
Let's assume x = 50, y = 70.
When you call finalScore(x, y) in main
  1. The value of x is copied to function finalScore's local variable midterm.
  2. The value of y is copied to function finalScore's local variable exam.
  3. The codes in finalScore's function body will be executed. and the result will be returned.
Key concept This implies Advantage Example1
What is the output of the following program?
#include<iostream>

using namespace std;

void increase(int); int main() { int x = 1; cout << "Before calling the increase function, x is " << x << ".\n"; increase(x); cout << "After calling the increase function, x is " << x << ".\n"; return 0; } void increase(int x) {
x++;
}
Output. The x is not increased!
Before calling the increase function, x is 1.
After calling the increase function, x is 1.
Example2
The user tries to write a function to ask for a postitive input. Here is his code
#include <iostream>
using namespace std;

void positiveInput(int num); int main() { int x = 0; positiveInput(x); cout << "x is " << x << endl; return 0; } void positiveInput(int num) {
do {
cout << "Please input a positive number : ";
cin >> num;
if(num <= 0) {
cout << "You can't input negative number.\n";
}
} while (num <= 0);
}
Here is the output. The x is unchanged!
Please input a positive number : -19
You can't input negative number.
Please input a positive number : -100
You can't input negative number.
Please input a positive number : 5 x is 0

Call by reference

Here are the new codes for the above examples
Example1
What is the output of the following program?
#include<iostream>

using namespace std;

void increase(int& x); int main() { int x = 1; cout << "Before calling the increase function, x is " << x << ".\n"; increase(x); cout << "After calling the increase function, x is " << x << ".\n"; return 0; } void increase(int& x) {
x++;
}
Output. The x is really increased by 1.
Before calling the increase function, x is 1.
After calling the increase function, x is 2.
Example2
#include <iostream>
using namespace std;

void positiveInput(int& num);

int main() {
int x = 0;
positiveInput(x);
cout << "x is " << x << endl;

return 0;
}

void positiveInput(int& num) { do { cout << "Please input a positive number : "; cin >> num; if(num <= 0) { cout << "You can't input negative number.\n"; } } while (num <= 0); }
Here is the output. x is changed!
Please input a positive number : -19
You can't input negative number.
Please input a positive number : -100
You can't input negative number.
Please input a positive number : 5 x is 5
Syntax
type& variableName
      
The ampersand sign, & comes right after the type.
or
type amp;variableName
      
The ampersand sign, & comes right before the variableName.
They are used in parameter list.
Example
int increase(int& num);
void askForInput(double &income, char &gender);
Comparison
call by value
call by reference
no & in the formal parameters
use & in the formal parameters
value of the variable is passed to the function
the address of the memory location of the variable is passed to the function
changes made to the local variable
changes made to the variable in the memory location.
no change can be made to the original variable
changes can be made the the original  variable because the original variable is the variable in the memory location.

Mixed Parameter list Example
#include <iostream>
using namespace std;

const double TAXRATE = 0.25;

// returns the tax
double tax(double& income, bool married); int main() { double charlesIncome, charlesTax; double amyIncome, amyTax; charlesTax = tax(charlesIncome, false); cout << "Charles' taxable income: " << charlesIncome <<endl << "Charles' tax: " << charlesTax << endl << endl; amyTax = tax(amyIncome, true); cout << "Amy's taxable income: " << amyIncome << endl << "Amy's tax: " << amyTax << endl << endl; return 0; } double tax(double& income, bool married) {
double income1;
double income2;
if(married) { // if married
cout << "Please enter your income: ";
cin >> income1;
cout << "Please enter your spouse's income: ";
cin >> income2;
income = income1+income2;
} else { // single
cout << "Please enter your income: ";
cin >> income;
}

return income*TAXRATE;
}

Output
Please enter your income: 100000
Charles' taxable income: 100000
Charles' tax: 25000

Please enter your income: 45000
Please enter your spouse's income: 62333
Amy's taxable income: 107333
Amy's tax: 26833.3
      

Function overloading

Example
#include<iostream>
using namespace std;

// returns the average of the two numbers n1 and n2
double ave(double n1, double n2); // returns the average of the three numbers n1, n2 and n3 double ave(double n1, double n2, double n3); int main() { cout << "The average of 2.0, 2.5, 3.0 is " << ave(2.0, 2.5, 3.0) << endl;

cout << "The average of 4.5 and 5.5 is "
<< ave(4.5, 5.5) << endl;

return 0;
}

double ave(double n1, double n2) {
return (n1+n2)/2;
}

double ave(double n1, double n2, double n3) {
return (n1+n2+n3)/3;
}
Output
The average of 2.0, 2.5, 3.0 is 2.5
The average of 4.5 and 5.5 is 5
Example1(the program can't be compiled)
#include<iostream>
using namespace std;

// returns the average of the two numbers n1 and n2
double ave(double n1, double n2); // returns the average of the three numbers n1, n2 int ave(double n1, double n2); int main() { cout << "The average of 2.0, 2.5 is " << ave(2.0, 2.5) << endl; cout << "The average of 4.5 and 5.5 is " << ave(4.5, 5.5) << endl; return 0; } double ave(double n1, double n2) { return (n1+n2)/2; } int ave(double n1, double n2) { return static_cast<int>(n1+n2)/2; // type casting }
How the computer determines which function definition to use?
  1. Exact match: if the number and types of arguments exactly match a definition, then that is the definition used.
  2. Match using automatic type conversion: If there is no exact match but there is a match using automatic type conversion, then the match is used. If two matches are found, then there is an ambiguous and an error message will be issued.
Automatic type conversion
Examples are : What is the output of the following program?
#include<iostream>
using namespace std;

void f(int n);
void f(int n, double m);
void f(double n, int m);
void f(char n, double m);

int main() {
int a = 1;
double b = 3.2;
char c = 'X';

f(a,b);
f(c,b);
f(b,a);
f(a);
f(b);
f(c);

return 0;
}

void f(int n) {
cout << "f(int n):"
<< " n is " << n << endl;
}

void f(int n, double m) {
cout << "f(int n, double m)" << endl;
}

void f(double n, int m) {
cout << "f(double n, int m)" << endl;
}

void f(char n, double m) {
cout << "f(char n, double m)" << endl;
}
Output
f(int n, double m)
f(char n, double m)
f(double n, int m)
f(int n): n is 1
f(int n): n is 3
f(int n): n is 88
The following function calls will cause compiling error(why?)