Lec4. C++ Function Parameters, Overloading, and Arrays
Lec4. C++ Function Parameters, Overloading, and Arrays
Function Parameters,
Overloading, and
Arrays
• Call-by-reference
– "address of" actual argument is passed
– Used to provide access to caller’s actual argument
– Caller’s data can be modified by called function!
– Typically used for input function
• To retrieve data for caller
• Data is then "given" to caller
– Specified by ampersand, &, after type in formal parameter list
4-2
Call-by-Value Example:
Formal Parameter Used as
a Local Variable
4-3
#include <iostream>
using namespace std;
int main () {
// declare simple variables
int i, j=10;
// declare reference variables
int & s = i;
i = 5;
cout << "Address of i : "<< &i<<endl;
cout << "Value of i : " << i << endl;
int& r = i;
cout << "Address of r : " << &r<<endl;
cout << "Value of r : " << r << endl;
r=j;
cout << "Address of i : "<< &i<<endl;
cout << "Value of i : " << i << endl;
i = 11;
cout << "Value of s : " << s << endl;
cout << "Value of r : " << r << endl;
return 0;
}
4-4
//Program to demonstrate call-by-reference parameters .Call-By-Reference
#include <iostream> Parameter
using namespace std; Example
void getNumbers(int& input1, int& input2); //Reads two integers from the keyboard .
void swapValues(int& variable1, int& variable2); //Interchanges the values.
void showResults(int output1, int output2); //Shows the values of output1 and output2 .
int main( ) {
int firstNum, secondNum;
getNumbers(firstNum, secondNum);
swapValues(firstNum, secondNum);
showResults(firstNum, secondNum);
return 0;
}
void getNumbers(int& input1, int& input2) {
cout << "Enter two integers: ";
cin >> input1 >> input2;
}
void swapValues(int& variable1, int& variable2){
int temp;
temp = variable1; Enter two integers: 5 6
variable1 = variable2;
In reverse order the numbers are: 6 5
variable2 = temp;
}
void showResults(int output1, int output2){
cout << "In reverse order the numbers are: "
<< output1 << " " << output2 << endl;
} 4-5
Call-By-Reference Details
• What’s really passed in?
– A "reference" back to caller’s actual argument!
• Refers to memory location of actual argument
• Called "address", which is a unique number referring to distinct place in memory
4-10
Overloading Resolution
• C++ function call resolution:
– 1st: looks for exact signature (Exact Match)
• Looks for exact signature where no argument conversion required
– 2nd: looks for "compatible" signature (Compatible Match)
• Looks for "compatible" signature where automatic type conversion is possible:
– 1st with promotion (e.g., intdouble) no loss of data
– 2nd with demotion (e.g., doubleint) Possible loss of data
4-13
Default
Arguments
Example:
4-14
Introduction to Arrays
• Array definition: a collection of data of same type
– Avoids declaring multiple simple variables
• Declare the array allocates memory
int score[5]; //Declares array of 5 integers named "score"
• In declaration, specifies SIZE of array
• Value in brackets called index or subscript: Numbered from 0 to size - 1
• Access using index/subscript
cout << score[3];
5-15
Array Program
Example:
Enter 5 scores:
5 9 2 10 6
The highest score is 10
The scores and their
differences from the highest are:
5 off by 5
9 off by 1
2 off by 8
10 off by 0
6 off by 4
5-16
Major Array Pitfall
• Array indexes always start with zero!
• Zero is "first" number to computer scientists
• C++ will "let" you go beyond range
– Unpredictable results
– Compiler will not detect these errors!
• Example: double t[24]; // 24 is array size
// Declares array of 24 double values called temperature
– They are indexed as: t[0], t[1] … t[23]
– Common mistake: t[24] = 5;
• Index 24 is "out of range"!
• No warning, possibly disastrous results
5-17
Defined Constant as Array Size
• Always use defined/named constant for array size
• Example: const int NUMBER_OF_STUDENTS = 5;
int score[NUMBER_OF_STUDENTS];
– Improves readability
– Improves versatility
– Improves maintainability
• Use everywhere size of array is needed
– In for-loop for traversal:
for (idx = 0; idx < NUMBER_OF_STUDENTS; idx++){
// Manipulate array
}
– In calculations involving size:
lastIndex = (NUMBER_OF_STUDENTS – 1);
– If size changes requires only ONE change in program!
5-18
Arrays in Memory
• Array declarations allocate memory for entire array
• Sequentially-allocated: Means addresses allocated "back-to-back"
– Allows indexing calculations
• Simple "addition" from array beginning (index 0)
5-19
Initializing Arrays
• As simple variables can be initialized at declaration:
int price = 0; // 0 is initial value
• Arrays can as well: int children[3] = {2, 12, 1};
– Equivalent to following: int children[3];
children[0] = 2;
children[1] = 12;
children[2] = 1;
• Auto-Initializing Arrays
– If fewer values than size supplied:
• Fills from beginning, fills "rest" with zero of array base type
– If array-size is left out
• Declares array with size required based on number of initialization values
• Example: int b[] = {5, 12, 11};
– Allocates array b to size 3
5-20
Arrays in Functions
• As arguments to functions
– Indexed variables: An individual "element" of an array can be function
parameter
– Entire arrays: All array elements can be passed as "one entity"
• As return value from function