1 / 8

what are executable/non-executable statements

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?

ninon
Download Presentation

what are executable/non-executable statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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?

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

  3. 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 }

  4. 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(void){ cout << "Again: [y/n] "; char answer; cin >> answer; if (answer == 'y') return true; else return false; } int main(void){ do cout << "Hello, World!\n"; while(again()); } how do you code a looping construct so that it continues if a boolean function returnsfalse rather than true?

  5. 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 a(void); void b(void); void c(void); int main(void){ a(); } void a(void){ b(); c(); } void b(void){ ; // does not do anything }   void c(void){ ; // does not do anything }

  6. 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, an ampersand (&) precedes the parameter declaration both in the function head and in the function prototype • the function call is not distinguishable (careful!) • prototype void get_intput(double &fard); // extended form void get_intput(double &); // abbreviated form • definition void get_intput(double &fard){ cout << “I will convert Fahrenheit Temperature ” << “to Celsius.\n” << “Enter temperature in Fahrenheit: “; << “ degrees Celsius.\n”; cin >> fard; } • mixing call kinds of parameters is allowed: myfunc(int&,double,int&);

  7. Call-by-Reference (Cont.) • function invocations for call-by-reference and call-by-value are similar but not the same: double f_temp; get_input(f_temp); • passing expressions by reference is not allowed! get_input(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 get_numbers(int& input1, int& input2); call: get_numbers(first_num, second_num); • passing one similar value as return and the other as parameter is bad style: prototype: int get_numbers(int& input2); // BAD call: first_num=get_numbers(second_num); // STYLE

  8. Call-by-Reference, Example • this function swaps the values of arguments void swapValues(int& var1, int& var2){ int temp; temp = var1; var1 = var2; var2 = temp; } • what is output when this code is executed? int i=1, j=2; swapValues(i,j); cout << i << ’ ’ << j;

More Related