Procedural Abstraction and Functions That Return a Value Savitch, 2007. Chapter 4 1
Procedural Abstraction: Functions I Top-Down Design Predefined Functions Programmer-Defined Functions Procedural Abstraction Local Variables Overloading Function Names 2
Top-Down Design Most programming languages provide for modular design: modules subroutines procedures functions These allow the programmer to break a complex task into smaller, more manageable modules: top-down design stepwise refinement divide and conquer iterative functional decomposition. 3
Example: The Mileage Program Mileage Program Display Compute Instructions Mileage Get Input Calculate Display Results Ask Again? Display Instructions Display Instructions Compute Mileage Get Input Calculate Display Results Again? 4
Predefined Functions C++ offers libraries of predefined functions. A header file must be included. Each has a single return type. Many require a list of one or more arguments. They are invoked (called) when encountered in expressions. The return value is used to evaluate the expression. 5
Example: The Square Root Function double sqrt(double number) input number The prototype delta = MAX_DOUBLE epsilon = MIN_DOUBLE quotient = number/2 return type name argument type parameter double sqrt(double number); delta > epsilon? new = number/quotient delta = new - quotient quotient = (new + quotient)/2 return quotient The invocation r = sqrt(n) ; 6
Some Math Functions Description Prototype Header File square root double sqrt(double n); cmath power double pow(double b, double e); cmath absolute value for int int abs(int n); cstdlib absolute value for long long labs(long n); cstdlib absolute value for double double fabs(double n); cmath ceiling double ceil(double n); cmath floor double floor(double n); cmath 7
A Function Call (Invocation) Header File Invocation 8
Type Casting To avoid truncation in division involving integers, force the literal to type double. Trouble occurs when neither operand is a literal. The solution lies in the type cast operator (C/C++). C++ provides the static cast: C++ also provides a functional notation: Be careful: 9
Programmer-Defined Functions referencing declaration (prototype) invocation defining declaration return to invocation argument to parameter 10
Declarations Declarations give meaning to identifiers. Defining declarations create storage for either data or algorithms. Referencing declarations refer to identifiers defined elsewhere. Prototypes are referencing declarations for functions (algorithms). 11
Arguments and Parameters An argument is a value of an expression passed to a function when it is invoked. Sometimes called an actual parameter. A parameter is a special local variable that is automatically initialized to the value of the argument passed to the function. Sometimes called a formal parameter. If an argument s type does not match that of the parameter, an automatic type conversion will occur if possible. Otherwise, a compiler error will result. 12
Golbal Constants //Computes the area of a circle and the volume of a sphere. //Uses the same radius for both calculations. #include <iostream> #include <cmath> const double PI = 3.14159; double area(double radius); //Returns the area of a circle with the specified radius. double volume(double radius); //Returns the volume of a sphere with the specified radius. 13
Local Namespaces int main( ) { using namespace std; double radius_of_both, area_of_circle, volume_of_sphere; } cout << "Enter a radius to use for both a circle\n" << "and a sphere (in inches): "; cin >> radius_of_both; area_of_circle = area(radius_of_both); volume_of_sphere = volume(radius_of_both); cout << "Radius = " << radius_of_both << " inches\n" << "Area of circle = " << area_of_circle << " square inches\n" << "Volume of sphere = " << volume_of_sphere << " cubic inches\n"; return 0; double area(double radius) { using namespace std; return (PI * pow(radius, 2)); } double volume(double radius) { using namespace std; return ((4.0 / 3.0) * PI * pow(radius, 3)); } 14
Procedural Abstraction The black box analogy input(s), if any results output(s), if any side effects, if any processing: iterative functional decomposition data algorithms Translation into a formal language Testing and debugging 15
Nuance One may deduce meaning from context Wash the shirt. Wash the car. Wash the dog. In everyday language, it s not necessary to say shirtwash the myshirt. carwash the Ford. dogwash Fido. 16
Name Overloading In C++ identifiers (names) may be loaded up with multiple meanings. The ambiguity is always resolved through context. The signature of a function is its name (identfier) plus the list of its parameter types. Function names may be overloaded (redefined) as long as the signatures differ. Differences in return type alone are not sufficient to constitute different signatures. Arguments are subject to automatic promotion and forced demotion. 17
Overloading Example 18
19