Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.

Similar presentations


Presentation on theme: "Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning."— Presentation transcript:

1 Chapter 6 User-Defined Functions I

2 Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning functions Actual parameters Formal parameters Construction, and Usage

3 What is a Function? Building blocks of a program Manageable subdivision of a program Often called modules Like a miniature program Designed to perform a specific task

4 Advantages of Using Functions Allow complicated programs to be divided into manageable pieces A programmer can focus on a function and construct it, debug it, and perfect it A large program may be divided amongst more than one programmer to work on simultaneously Can be used in more than one place in the program or even in different programs

5 Types of Functions Void Functions Do not have a return type Value-Returning Functions Has a data type The data type of a function is determined by the type of data returned

6 Predefined Functions Organized into separate libraries Math functions are in the cmath header I/O functions are in the iostream header To use you must: Include the correct header file Know the name of the function Know the number of parameters, if any Know the data type of each parameter Know the data type of the value computed by the function, called the type of the function

7 Predefined Functions (continued) Some examples of predefined mathematical functions are: pow(x, y) sqrt(x) floor(x) abs(x) To see a full list type “cmath header” into the Visual Studio help index

8 The pow Function pow(x,y) calculates x y, pow(2,3) = 8.0 pow returns a value of type double x and y are called the parameters (or arguments) of the function pow The function pow has two parameters To determine the types of parameters the pow function requires see the help documentation.

9 The sqrt Function sqrt(x) calculates the non-negative square root of x for x >= 0.0 sqrt returns a value of type double The function sqrt has only one parameter, of type double

10 The floor Function floor(x) calculates the largest whole number not greater than x floor returns a value of type double The function floor has only one parameter, of type double

11 Value-Returning Functions The value returned by a function is unique A value returning function may be used in an assignment or an output statement To make using a function worthwhile we must do one or more of the following: Save the value returned by the function Use the value in some calculation Print the value

12 Value-Returning Functions Components of the function definition: Name the function Number and type of parameters Type of the function (return type) The code required to accomplish the task (the body of the function)

13 Value-Returning Functions (contd.) Heading: made of of the name of the function, number and type of parameters, type of the function Formal Parameter: variable declared in the function heading Actual Paramter: variable or expression listed in a call to a function

14 Value-Returning Function (contd.) Syntax: functionType FunctionName (formal parameter list) { statements } functionType : the type of the value returned by the function; also called the data type.

15 Syntax The syntax of the formal parameter list is: dataType identifier, dataType identifier The syntax for a function call is: FunctionName(actual parameter list) The syntax for the actual parameter list is: expression or variable, expression or variable

16 Functions The formal parameter list can be empty If the formal parameter list is empty The parenthesis are still needed Function heading of the value-returning function takes the following form functionType FunctionName ( ) In a function call the actual parameter list is empty and takes the form FunctionName( )

17 Value-Returning Functions To call a value-returning function: Use its name, with the actual parameters (if any) in parenthesis Remember that there is a one-to-one correspondence between actual parameters and formal parameters

18 Value-Returning Functions (contd.) A value-returning function is called in an expression Expression may be part of an assignment statement or an output statement A function call in a program results in the execution of the body of the called function

19 The return Statement Once the function computes the value, the function returns the value via the return statement. When a return statement executes Function immediately terminates Control goes back to the caller When a return statement executes in the function main, the program terminates. The syntax of a return statement is: return expression;

20 Example double Larger ( double x, double y ) { if ( x >= y ) return x; return y; } Function type: double Formal Parameters: x and y

21 Function Prototype Function Prototype: function heading without the body of the function functionType FunctionName (parameter list); It is not necessary to specify the variable name in the parameter list of the function prototype The data type of each parameter must be specified

22 Example double Larger ( double x, double y ) { if ( x >= y ) return x; return y; } Function Prototype double Larger ( double x, double y ); or, double Larger ( double, double );

23 Flow of Execution Execution always begins in the function main no matter where main is placed in the program Other functions are executed only when they are called

24 Flow of Execution (continued) A function call statement results in the transfer of control to the first statement in the body of the called function After the last statement of the called function is executed, control is transferred back to the point immediately following the function call

25 Flow of Execution (continued) A value-returning function returns a value After executing the function, the value the function returns replaces the function call statement.

26 Putting everything together… #include using namespace std; // function prototypes double Larger ( double, double ); int main ( ) { double num1 = 32.045; double num2 = 17.213; double largest = 0.0; // function calls largest = Larger ( num1, num2 ); return 0; } // function definitions double Larger ( double x, double y ) { if ( x >= y ) return x; return y; }

27 Programming Example Write a function that returns the length of the hypotenuse of a right triangle given the lengths of the two legs a and b. To calculate the length of the hypotenuse use the pythagorean theorem a 2 + b 2 = c 2 where a and b are the lengths of the legs of the right triangle and c is the length of the hypotenuse.

28 Programming Example (contd.) c = The length of the hypotenuse is calculated by using the formula

29 Programming Example (contd.) Prototype: double CalculateHypotenuse ( double a, double b ); Definition: double CalculateHypotenuse ( double a, double b ) { double hypotenuse = 0.0; hypotenuse = sqrt(pow(a, 2.0) + pow(b, 2.0)); return hypotenuse; }

30 Using the CalculateHypotenuse Function in a Program #include using namespace std; double CalculateHypotenuse ( double, double ); int main ( ) { double sideA = 0.0; double sideB = 0.0; cout << “Please enter the lengths of the sides of the” << “right triangle, a and b: ” << endl; cin >> sideA >> sideB; cout << fixed << showpoint << setprecision(3) << endl << endl << “The length of the hypotenuse of the right triangle whose\nlegs are lengths ” << sideA << “ and ” << sideB << “ is ” << CalculateHypotenuse(sideA, sideB); return 0; } double CalculateHypotenuse ( double a, double b ) { double hypotenuse = 0.0; hypotenuse = sqrt ( pow ( a, 2.0 ) + pow ( b, 2.0 ) ); return hypotenuse; }

31 Lab Assignment Programming Exercise #6, Pg. 343

32 Homework Assignment Exercise #1, Pg. 337 Exercise #5, Pg. 338 Programming Exercise #5, Pg. 343


Download ppt "Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning."

Similar presentations


Ads by Google