Presentation is loading. Please wait.

Presentation is loading. Please wait.

L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);

Similar presentations


Presentation on theme: "L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);"— Presentation transcript:

1 l what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int); l what is a header file? l what is the difference between the two statements? #include and #include ”filename.h” l why are programs included in multiple files what are object files and how are they related to multiple file-program l what is linking? l what is multiple inclusion protection and why is it needed? Multiple Files Revisited 1

2 Programmer-Defined Functions II Void Functions, Predicates Program Stack, Call-by-Reference

3 l void function – does not return a value, can be used in a standalone statement void is specified as return type return -statement is not necessary; if used, cannot contain expression l frequently void functions are output functions: void show_results(double fard, double celd){ cout << fard << ” degrees Fahrenheit is equivalent to ” << celd << ” degrees Celsius.\n”; return; // not necessary } Void Functions 3

4 l predicate – function whose return value is boolean l used to compute a binary decision l 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') return true; else return false; } Predicates int main(){ do cout << "Hello, World!\n"; while(again()); } how do you code looping construct so that it continues if boolean function returns false rather than true ? 4

5 l program (call) stack – means of RAM allocation for local function variables. l last-in/first-out (LIFO) data structure l function frame – unit of allocation n contains local variables, parameters, return value void a(); void b(); void c(); int main(){ a(); } void a(){ b(); c(); } Program Stack void b(){ ; // does not do anything } void c(){ ; // does not do anything } 5

6 l what was call-by-value? l 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 n function call is not distinguishable l 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&); Call-by-Reference 6

7 l function invocations for call-by-reference and call-by-value are similar but not the same: double temp; getInput(temp); l passing expressions by reference is not allowed! getInput(23.0); // WRONG! l in call-by-reference the function operates on the memory location of the argument l 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); l 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 Call-by-Reference (Cont.) 7

8 l 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; Call-by-Reference, Example 8


Download ppt "L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);"

Similar presentations


Ads by Google