User Defined Functions Chapter 6 2 Chapter Topics

  • Slides: 15
Download presentation
User Defined Functions Chapter 6

User Defined Functions Chapter 6

2 Chapter Topics n n n Standard (Predefined) Functions User-Defined Functions Value-Returning Functions The

2 Chapter Topics n n n Standard (Predefined) Functions User-Defined Functions Value-Returning Functions The return Statement Function Prototype Flow of Execution

3 Top-Down Structured Design with Functions n n Recall two types of functions Value

3 Top-Down Structured Design with Functions n n Recall two types of functions Value returning function n n Void function (procedure) n n n computes a single value returns value to calling code uses return command called as a statement executes some task This chapter focuses on the value returning

4 Advantages of Using Functions 1. To help make the program more understandable 2.

4 Advantages of Using Functions 1. To help make the program more understandable 2. To modularize the tasks of the program n building blocks of the program 3. Write a module once n those lines of source code are called multiple times in the program

5 Advantages of Using Functions 4. While working on one function, you can focus

5 Advantages of Using Functions 4. While working on one function, you can focus on just that part of the program n n n construct it, debug it, perfect it. 5. Different people can work on different functions simultaneously. 6. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times

6 Standard (Predefined) Functions n Predefined functions n n n Part of the C++

6 Standard (Predefined) Functions n Predefined functions n n n Part of the C++ language Provided in function libraries Examples: abs(x), n Make sure to use the required #include file sin(x), log(x), pow( x, n) These functions will return a value n n n To be printed cout << sin (x); To be assigned y = pow (3, 4. 5); To be used in an expression 3. 14 * sqr(r)

Predefined Functions View sample program 7

Predefined Functions View sample program 7

8 Value-Returning Functions All the properties Information together provided make up the definition of

8 Value-Returning Functions All the properties Information together provided make up the definition of the by the heading of the use a function you have function For the compiler to written, it must know when it finds that function call in your source code … 1. The name of the function Information provided in 2. The number of parameters, if any the body of the function 3. The data type of each parameter 4. Data type of the value returned by the function 5. The code required to do the calculation

9 Value-Returning Functions n Consider a function for the area of a circle: double

9 Value-Returning Functions n Consider a function for the area of a circle: double circle. Area (double radius) { return 3. 14159 * radius; } n Note the n n n Heading (type, name, parameters) The body The return statement

10 Parameters Function definition syntax: n function. Type function. Name (formal parameter list) {

10 Parameters Function definition syntax: n function. Type function. Name (formal parameter list) { statements Parameters in the } declaration : formal parameters n Call (invocation of the function) cout << "Enter radius for circle area -> "; cin >> radius; area = circle. Area (radius); Parameters in the call: actual parameters

The return Statement n A value returning statement must have a return statement n

The return Statement n A value returning statement must have a return statement n n Else a warning from the compiler Also the function will actually return a "garbage" value Syntax: return expression; View example 11

12 Function Prototype n Recall that the compiler must know certain things about your

12 Function Prototype n Recall that the compiler must know certain things about your function n n When it finds a function call in your source code Must know information in heading Your program must have at least the heading of a function before it is invoked n Usually listed before function main ( ) View example

13 Alternative to Prototype n n n Also possible to place whole function definition

13 Alternative to Prototype n n n Also possible to place whole function definition before main () This is a requirement in some languages (Pascal) Either is acceptable in this class There may be a standard required of programmers within a particular organization View Example

14 Flow of Control n n First statement executed in any program is the

14 Flow of Control n n First statement executed in any program is the first statement in the function main( ) When another function called n n n logical control passed to first statement in that function’s body program proceeds through sequence of statements within the function When last statement of function executed n n control returns to where function was called control given to next command after the call

15 Flow of Control void main ( ) {. . . print_summary (rpt_total); revenue

15 Flow of Control void main ( ) {. . . print_summary (rpt_total); revenue = rpt_total *. 72675; . . . } void print_summary (int total) {. . . cout <<. . . - first statement } of main - function call, jumps to first statement of that function - proceeds through function - returns to next statement after call