Chapter 6 - Notes User-Defined Functions I I. Standard (Predefined) Functions A. A sub-program that performs a special or specific task and is made available by pre-written libraries in header files. B. Table of some Predefined Functions Function Standard Header File Purpose Parameter(s) Type Result abs(x) <cstdlib> Returns the absolute value of its argument: abs(-7) = 7 ceil(x) Returns the smallest whole number that is not less than x: ceil(56.34) = 57.0 cos(x) Returns the cosine of angle x: cos(0.0) = 1.0 exp(x) x Returns e, where e = 2.718: exp(1.0) = 2.71828 (radians) fabs(x) Returns the absolute value of its argument: fabs(-5.67) = 5.67 floor(x) Returns the largest whole number that is not greater than x: floor(45.67) = 45.00 pow(x,y) y Returns x ; if x is negative, y must be a whole number: pow(0.16, 0.5) = 0.4 tolower(x) <cctype> Returns the lowercase value of x if x is uppercase; otherwise, returns x toupper(x) <cctype> Returns the uppercasevalue of x if x is lowercase; otherwise, returns x Page 1
C. Examples of Predefined functions used in Programs #include <iostream> #include main ( ) base, expo, answer ; cout << "Please enter two eger numbers: " ; cin >> base >> expo ; answer = pow( base, expo ) ; cout << endl ; cout << base << " taken to the power of " << expo ; cout << " equals " << answer << endl << endl ; system("pause") ; return 0 ; OUTPUT Please enter two eger numbers: 2 4 2 taken to the power of 4 equals 16 Press any key to continue... NOTE that the above pre-defined function returns a value. Therefore, something must be done with the value. In the case of the example, the returned value is saved o the variable answer. Another way to use a pre-defined function that returns a value is to put it o a cout statement like the following example: Page 2
#include <iostream> #include main ( ) base, expo ; cout << "Please enter two eger numbers: " ; cin >> base >> expo ; cout << endl ; cout << base << " taken to the power of " << expo ; cout << " equals " << pow( base, expo ) << endl << endl ; system("pause") ; return 0 ; OUTPUT Please enter two eger numbers: 2 4 2 taken to the power of 4 equals 16 Press any key to continue... II User-Defined Functions A. Two Main Types 1. Value-Returning Functions ( functions that have a data type ) i. A function that may or may not use parameters, but always returns a single value to the calling function. 2. Void Functions ( functions that do not have a data type ) i. A function that may or may not use parameters, but does not return a value to the calling function. Page 3
III. Value-Returning Functions A. One of three (3) ways to use: 1. Save the value to memory in an assignment statement 2. Use the value in an expression 3. Pr the value to the screen or output file B. Parts of the Function 1. The Header: A single line where the data type of the function is listed first, then the name of the function, then inside of parenthesis, the are found (if any). Example of Header: sumit ( X, Y ) data type of parameter function name function data type 2. The Body: The statements used to perform the function. This consists of any legal C++ statements that can be used in the body of the main function. Declaration statements, assignment statements, Input/Output statements, and even other function calls are all found in the body of the function. Example of Body: function header sumit ( X, Y ) sum ; sum = X + Y ; function body return sum ; Page 4
3. Function Definition: The function definition consists of both the function header and function body. C. Syntax #include <iostream> area ( x_length, y_width ) return x_length * y_width ; ; function header body of function main () length, width, tot_area ; cout << "Please enter the length and width of a rectangle here: "; cin >> length >> width ; tot_area = area( length, width ); cout << endl; function called actual parameters cout << "The rectangle's area is " << tot_area << " square inches"; cout << endl << endl ; system("pause"); return 0; O R You can write functions with a prototype and define the function after the main program like it is done in your book and the following page. Page 5
#include <iostream> area ( x_length, y_width ); prototype main () length, width, tot_area ; cout << "Please enter the length and width of a rectangle here: "; cin >> length >> width ; tot_area = area( length, width ); cout << endl; function called actual parameters cout << "The the rectangle's area is " << tot_area << " square inches"; cout << endl << endl ; system("pause"); return 0; area ( x_length, y_width ) return x_length * y_width ; function header body of function D. Syntax for Formal Parameter List 1. datatype name ( data type identifier, data type identifier,... ) Examples: float divideit ( numerator, float denominator ) sumit ( num1, num2, num3, num4 ) bool islarger ( num1, num2 ) E. Syntax for Function Call 1. functionname ( actual parameter list ) Page 6
F. Syntax for Actual Parameter List 1. ( expression or variable, expression or variable,... ) Examples: divideit ( 4, 5 ) sumit ( var1, (10 + 2) / 6, var3 * 2, divideit ( 6, 3 ) ) islarger ( 5.34, var7 ) 2. Notice that the actual parameter list can consist of regular numbers (called literals), a variable that stores a value, a mathematical expression that returns a single value, or even another value returning function. 3. ALSO - A function does not have to receive parameters. In this case the function would be called with just the name of the function and an open and close parenthesis, with nothing between the parenthesis. Or Example: dothis ( ) or dothis ( void ) G. The return Statement 1. Once the function computes the value to be returned, the function returns this value via the return statement. 2. Syntax for return Statement return expression ; 3. An expression can be anything that returns a single value: a number (literal), a mathematical expression, a variable, or even another valuereturning function. 4. REMEMBER: the data type of the value being returned must match the data type of the function. Page 7
H. Function Prototype 1. Defined as the function heading without the body of the function. It is followed by a semicolon. 2. When defining a function after the main (function) program, a prototype must be used before the main (function) program begins. This is because, like variables, a function must be declared before it can be used. 3. Syntax of a Prototype datatype functionname ( ) ; #include <iostream> area ( x_length, y_width ); prototype main () length, width, tot_area ; cout << "Please enter the length and width of a rectangle here: "; cin >> length >> width ; tot_area = area( length, width ); cout << endl; function called actual parameters cout << "The the rectangle's area is " << tot_area << " square inches"; cout << endl << endl ; system("pause"); return 0; area ( x_length, y_width ) return x_length * y_width ; function header body of function Page 8