Presentation is loading. Please wait.

Presentation is loading. Please wait.

User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.

Similar presentations


Presentation on theme: "User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers."— Presentation transcript:

1 User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers

2 Why Define Functions? Break complex tasks into simpler subtasks Avoid writing one piece of code more than once (enhances maintenance) Use names for tasks (enhances readability and organization)

3 Function Declarations Provide enough information to use a function Do not reveal implementation details Occur above the main function or in a library header file

4 Example Declaration // Function: cube // Computes the cube of a number // Input: a real number // Output: a real number representing the cube // of the input double cube(double n); Use a prefatory comment to describe the task, the inputs (parameters), and output (return value)

5 Example Implementation double cube(double n) { return n * n * n; } Can occur below or above the main function or in a library implementation file

6 Example Use in Complete Program #include double cube(double n); int main() { cout << cube(3) << endl; return 0; } double cube(double n) { return n * n * n; }

7 Formal and Actual Parameters A formal parameter appears in the functions header (definition) An actual parameter appears in the functions call (use)

8 #include double cube(double n); int main() { cout << cube(3) << endl; return 0; } double cube(double n) { return n * n * n; } actual parameter formal parameter Formal and Actual Parameters

9 #include double cube(double n); int main() { cout << cube(3 + 5) << endl; return 0; } double cube(double n) { return n * n * n; } actual parameter formal parameter Formal and Actual Parameters

10 Return Values and void Functions If a function has a single output datum, use a return statement with that value and declare the appropriate return type If a function has no output data or more than one output datum, use the void return type void displayResults(double total) double cube(double n)

11 Value and Reference Parameters Use a value parameter when the data should be input-only Use a reference parameter when the data should be input/output

12 Value Parameters The computer –evaluates the actual parameter –places a copy of that value in a temporary memory location –leaves the memory location of the actual parameter unchanged double x = 9.5; double y = cube(x); double cube(double n);

13 Reference Parameters The computer –treats the formal parameter as an alias for actual parameters memory location –allows uses and modifications of the value in that location through the formal parameter double x, y; getData(x, y); void getData(double &a, double &b);

14 Constant Reference Parameters The computer –treats the formal parameter as an alias for actual parameters memory location –allows uses but no modifications of the value in that location through the formal parameter double x; getData("Enter a number:", y); void getData(const apstring &a, double &b);

15 The Scope of a Variable The scope of a variable is the area of program text in which the variable is associated with a particular value Nested blocks {} introduce local scopes Visibility extends inwards but not outwards

16 #include double cube(double n); int main() { int x = 3; cout << cube(x) << endl; return 0; } double cube(double n) { return n * n * n; } Scope and Contour Diagrams Outermost block contains global names (cube) They are visible in nested blocks

17 #include double cube(double n); int main() { int x = 3; cout << cube(x) << endl; return 0; } double cube(double n) { return n * n * n; } Scope and Contour Diagrams Nested blocks contain local names They are visible in the block where theyre declared

18 #include double cube(double n); int main() { int x = 3; cout << cube(x) << endl; return 0; } double cube(double n) { int x = n * n * n; return x; } Scope and Contour Diagrams Parameters and local variables are visible only in nested blocks The variable x refers to two distinct memory locations in separate blocks

19 // Library header file: myinput.h #ifndef MY_INPUT #define MY_INPUT #include "apstring.h" // Function: getInteger // Prompts user for an integer, inputs it, and returns it // // Input: A string representing the prompt // Output: The integer input by the user int getInteger(const apstring &prompt); // Function: getString // Prompts user for a string, inputs it, and returns it // // Input: A string representing the prompt // Output: The string input by the user apstring getString(const apstring &prompt); #endif Library Header File

20 // Library implementation file: myinput.cpp #include "myinput.h" int getInteger(const apstring &prompt) { int data; cout << prompt; cin >> data; return data; } apstring getString(const apstring &prompt) { apstring data; cout << prompt; cin >> data; return data; } Library Implementation File


Download ppt "User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers."

Similar presentations


Ads by Google