1 / 9

Functions 2.

Functions 2. 1. Review. 2. Adding functions. 3. In-class exercise on interface design. 1. Review. For user-defined functions in C++, we need 3 items: (1) a function prototype. Where is it? What does it include? What is it for? (2) a function call.

von
Download Presentation

Functions 2.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions 2. 1. Review. 2. Adding functions. 3. In-class exercise on interface design.

  2. 1. Review. • For user-defined functions in C++, we need 3 items: • (1) a function prototype. Where is it? What does it include? What is it for? • (2) a function call. • (3) the function (implementation) itself. Where is it placed?

  3. Review (cont.) • Modules (such as main and another function) communicate via parameters. • The parameters in the function call are called ACTUAL parameters. • The parameters in the function heading are called FORMAL parameters. • A function call can send parameters to a function and receive results from the function.

  4. Review (cont.) • There are 2 types of formal parameter. • (1)A value parameter receives a copy of the actual parameter, but the copy has its own address. Therefore changing the copy does not change the original. See diagram. • (2) An address parameter receives the address of the actual parameter. So changing an address parameter does change the original. See diagram.

  5. Review (cont.) • Analogy: • The value parameter is like a copy of a birth certificate. No matter what happens to the copy, the original is unchanged. • An address parameter is like an alias (another name for the same person). Anything you do to Superman you do to Clark Kent (2 names for the same person).

  6. 2. Adding functions. • A helpful way to begin is to think about what changes are needed to go from a program with no functions to a program with them. • To design functions, we first need to think carefully about the interface (what needs to be sent to and returned from the function). Then we need to design the implementation.

  7. Adding functions (cont.). • See the example NOFUNC.CPP which is analyzed into Input, Calculate and Display tasks, each of which can be coded as a function. • First we can design the interface. This enables us to complete the function prototype, function call and function heading.

  8. Adding functions (cont.). • Then we can add the implementation (code inside each function). • This then gives us the “functionalized” (or funky) version of the program, WITHFUNC.CPP.

  9. 3. In-class exercise on interface design. • See the incomplete code fragment PARAMS.CPP, which will form the basis of program 5. • Read the specification in the comments. • Working in small groups, complete the interface only (that is the prototype, function calls and function headings).

More Related