1 / 9

Functions

Functions. Robert Reaves. Functions. Interface – the formal description of what a subprogram does and how we communicate with it Encapsulation – Hiding a module implementation in a separate block with a formally specified interface.

gus
Télécharger la présentation

Functions

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. Functions Robert Reaves

  2. Functions • Interface – the formal description of what a subprogram does and how we communicate with it • Encapsulation – Hiding a module implementation in a separate block with a formally specified interface. • Module parameter list – a set of variables within a module that hold values that are either incoming to the module or outgoing to the caller, or both. • Data flow – the direction of flow of information between the caller and a module through each parameter.

  3. Communicating with a module • Incoming values are values received from the caller. • Outgoing values are values produced and returned to the caller. • Incoming/outgoing values are values that the caller has that the module changes. (receives and returns.)

  4. Writing functions • void x(); • This would come before main and is called the function prototype. • Must include the data types of the parameters for this function. • x(); • This would be invoked where ever you needed to call your function, called function call. • void x() { • // some code. • } • This would come after main and the “some code” would be what your function did. Called the function definition.

  5. Void Functions • Do NOT return a value to caller. • void x() { // after the name is the parameter list, this one is empty. • // Some code here… • } • “void” signals the compiler that this is not a value-returning function. • Can use inside function: • return; // returns to the caller of the function immediately.

  6. Function Parameters • The code between parentheses that looks like a variable declaration, called parameter declaration. • void x(int y, int t) { // int y and int t would be the parameter declaration. • // some code. • } • Argument is a variable or expression listed in a call to a function. • x(height, width); // height and width would be arguments. • Parameter is a variable declared in a function definition.

  7. Local Variables • Local variable isa variable declared within a block and not accessible outside of that block. • void x(); • int main() { • int x = 5; // local variable to main. • x(); • return 0; • } • void x() { • cout << x << endl; // will say x is undefined, x is local to main. • }

  8. Parameters • Value parameter – a parameter that receives a copy of the value of the corresponding argument. • void x(int y); // y is a value parameter. • Reference parameter – a parameter that receives the location (memory address) of the caller’s argument. • void x(int& y); // y is a reference parameter.

  9. Documentation • Use preconditions and post-conditions as comments to document function interfaces. • Pre is an assertion describing everything the function requires to be true at the moment the caller invokes the function. • Post describes the state of the program the moment the function finishes executing. • // Pre: sum has been assigned and count is greater than 0. • // Post: The average has been output on one line. • void PrintAverage(float sum, int count) { • cout << “Average is “ << sum / float(count) << endl; • }

More Related