Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr.

Similar presentations


Presentation on theme: "Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr."— Presentation transcript:

1 Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr. Ming Zhang

2 Function Prototype A function prototype tells the compiler * the name of the function, * the type of data returned by the function, * the number of parameters the function expects to receive, * the types of the parameters, and * the order in which these parameters are expected. Dr. Ming Zhang

3 Example 1 of Function Prototype int maximum( int, int, int); * the name of the function: maximum * the type of data returned by the function: int * the number of parameters the function expects to receive: 3 * the types of the parameters: int * the order in which these parameters are expected: (int, int, int) Dr. Ming Zhang

4 Example 2 of Function Prototype float minimum( int, int, int, float); * the name of the function: minimum * the type of data returned by the function: float * the number of parameters the function expects to receive: 4 * the types of the parameters: int and float * the order in which these parameters are expected: (int, int, int, float) Dr. Ming Zhang

5 Promotion Rules * The promotion rules apply to expressions containing values of two or more data types (mixed-type expression). * The type of each value in a mixed-type expression is promoted to the highest type in the expression ( Actually a temporary version of each value is created and used for the expression - the original values remain unchanged). Dr. Ming Zhang

6 Common Use of Promotion * A common use of promotion is when the type of an argument to a function does not match the parameter type specified in the function definition. * The type of an argument to a function is promoted to the highest parameter type specified in the function definition. Dr. Ming Zhang

7 Date Type (from highest to lowest ) Data Type Minimum Maximum Long double system defined double system defined float system defined unsigned long int 0 4294967295 long int -2147483647 +2147483647 unsigned int 0 65535 int -32767 +32767 unsigned short int 0 65535 short int -32767 +32767 unsigned char 0 255 char -127 +127 Dr. Ming Zhang

8 Library Header Files * Each Standard library has a corresponding header file containing the function prototypes for all the functions in the library and definition of various data types and constants needed by those functions. * The header files ending in “.h” are “old-style’ header files. * The new-style header files do not use “.h”. Dr. Ming Zhang

9 Examples of Old-Style Header Files * Contains the floating-point size limits of the system. New version is. * Contains function prototypes for math library functions. New version is * Contains function prototypes for the standard input/output library functions and information used by them. New version is ……….. Dr. Ming Zhang

10 Examples of New-Style Header Files * Contains classes and functions used by the standard library to allocated memory to the standard library containers. * Contains the definition of class string from the standard library. * Contains a class for defining the numerical data type limits on each computer platform. …………….. Dr. Ming Zhang

11 Custom Header Filers * The programmer can create custom header files. * Programmer-defined header files should end in “.h”. * A programmer-defined header file can be included by using the #include preprocessor. * Example #include “square.h” Dr. Ming Zhang

12 Fahrenheit & Celsius (1)- Case Study #include #define MAXCOUNT 4 using std:cout; using std::cin; using std::endl; int main( ) { int count, fahren; float tempvert(int); for(count =1; count <=MAXCOUNT, count++) { cout << “Enter a Fahrenheit temperature”<<endl; cin >> fahren; cout << “The Celsius equivalent is” << tempvert(fahren) << endl; } Dr. Ming Zhang

13 Fahrenheit & Celsius (2)- Case Study /* Convert Fahrenheit to Celsius*/ float tempvert(int intemp) { return( ( intemp - 32) * (5.0 /9.0) ); } Dr. Ming Zhang

14 Exercise/Home Work 1 For the following function headers, determine the number, type, and order (sequence) of the values that must be passed to the function: (1) void factorial( int n) (2) void price( int type, double yield, double maturity) (3) void yield(int type, double price, double maturity) (4) void interest(char flag, float price, float time) Dr. Ming Zhang

15 Exercise/Home Work 2 Write a function named find_abs that accepted a double-precision number passed to it, computes its absolute value, and display the absolute value. The absolute value of a number is the number itself is the number is positive, and the negative of the number it the number is negative. Dr. Ming Zhang

16 Exercise/Home Work 3 * A second-degree polynomial in x is given by the expression ax 2 + bx + c, where a, b, and c are know numbers and a is not equal to zero. Write a C++ function named poly_two(a, b, c, x) that computes and returns the value of a second-degree polynomial for any passed integer values of a, b, c, and x. And also write a C++ working program to input a, b, c, and x. And print out the result of polynomial function. Dr. Ming Zhang


Download ppt "Function 2 (L17) * Function Prototype * Promotion Rules * Data Type * Library Header File * Customer Header File * Case Study * Exercise/Home Work Dr."

Similar presentations


Ads by Google