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

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:

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

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)

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

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)

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

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; }

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

#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

#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

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)

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

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);

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);

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);

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

#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

#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

#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

// 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

// 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