Multiple Files Revisited what are executable/non-executable statements out of the ones below which statements are executable #include <iostream> P=3.14; const double PI=3.14; int myfunc(int); what is a header file? what is the difference between the two statements? #include <filename> and #include ”filename.h” why are programs included in multiple files what are object files and how are they related to multiple file-program what is linking? 1 Programmer-Defined Functions II Void Functions, Boolean Functions Program Stack, Call-by-Reference Void Functions void function – does not return a value, can be used only as a standalone statement void is specified as return type return-statement is not necessary; if used, cannot contain expression frequently void functions are output functions: void show_results(double fard, double celd){ cout << fard << ” degrees Fahrenheit is equivalent to\n” << celd << ” degrees Celsius.\n”; return; // not necessary } 3 Boolean Functions boolean function – return value is boolean used to compute a binary decision idiom – use a boolean function as an expression in a looping or branching construct bool again(){ cout << "Again: [y/n] "; char answer; cin >> answer; if (answer == 'y') int main(){ return true; do else cout << "Hello, World!\n"; while(again()); return false; } } how do you code looping construct so that it continues if boolean function returns false rather than true? 4 Program Stack program (call) stack – means of RAM allocation for local function variables. last-in/first-out (LIFO) data structure function frame – unit of allocation contains local variables, parameters, return value void b(){ void a(); ; // does not do anything void b(); } void c(); int main(){ a(); } void c(){ ; // does not do anything } void a(){ b(); c(); } 5 Call-by-Reference what was call-by-value? call-by-reference is a parameter passing discipline that allows the function to modify the arguments to distinguish call-by-reference ampersand (&) precedes parameter declaration both in function head and in function prototype function call is not distinguishable prototype void getIntput(double &fard); // extended form void getIntput(double &); // abbreviated form definition void getIntput(double &fard){ cout << ”I will convert Fahrenheit Temperature ” << ”to Celsius.\n” << ”Enter temperature in Fahrenheit: ”; << ” degrees Celsius.\n”; cin >> fard; } mixing calls of parameters is allowed: myfunc(int&, double, int&); 6 Call-by-Reference (Cont.) function invocations for call-by-reference and call-by-value are similar but not the same: double temp; getInput(temp); passing expressions by reference is not allowed! getInput(23.0); // WRONG! in call-by-reference the function operates on the memory location of the argument functions that need to return more than one value usually use call-by-reference: prototype: void getNumbers(int& input1, int& input2); call: getNumbers(one, two); passing one similar value as return and the other as parameter is bad style: prototype: int getNumbers(int& input2); // BAD call: one=getNumbers(two); // STYLE 7 Call-by-Reference, Example this function swaps the values of arguments void swapValues(int& left, int& right){ int temp; temp = left; left = right; right = temp; } what is output when this code is executed? int i=1, j=2; swapValues(i,j); cout << i << ’ ’ << j; 8