1 / 8

C++ Functions Revisited

INFSY 307 C++. C++ Functions Revisited. Must always consider three parts of the Program: Function Prototype: Can be in the header or top of a function (including main) Function Call: May be in ANY function

lara-morse
Download Presentation

C++ Functions Revisited

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. INFSY 307 C++ C++ Functions Revisited

  2. Must always consider three parts of the Program: • Function Prototype: Can be in the header or • top of a function (including main) • Function Call: May be in ANY function • (main ( ) or any other) • Function Header: Indicates the top of the function.

  3. C++ Functions • Function Prototype void get_number (); • Function Call m.get_number (); • Function void math::get_number () Note: 1) format of each! 2) typing and argument consistency!

  4. C++ Function – given a return value • Function Prototype int get_number (); • Function Call int x; x=m.get_number (); • Function int math::get_number () { int num; num=read_char_to_int (); return num; }

  5. C++ Functions • Function name • Name must correspond to name in prototype • Function type must correspond to type in prototype • Local variables only available during function execution/global variables do not lose value int math::get_number () { int num; num=read_char_toint (); return num; }

  6. C++ Functions CONSIDERATIONS! • Provide function PRE and POST condition comments under EACH the prototype Others must know what is the beginning state of associated variables, what the function does, and what to expect as complete after execution.

  7. Function Arguments: • Are contained in each critical component: • Function call • Function prototype • Function Heading • Must match as far as: • type • order

  8. C++ Sample Function with argument list and a return value void samp::output (double value, int years, char name []) { cout <<setw (20)<<“ “<< “Name is: “<<name<<endl; cout<<setw (20)<<“ “<<“Years in Program: “years<<endl; cout<<setw (20)<<“ “<<“GPA: “<<value<<endl; return; } int main () samp s; char name []; int yrs; double gpa; s.output (gpa, yrs, name); //assume are defined .

More Related