Functions 2
This presentation is the property of its rightful owner.
Sponsored Links
1 / 9

Functions 2. PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on
  • Presentation posted in: General

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.

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Functions 2

Functions 2.

1. Review.

2. Adding functions.

3. In-class exercise on interface design.


1 review

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?


Review cont

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.


Review cont1

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.


Review cont2

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).


2 adding functions

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.


Adding functions cont

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.


Adding functions cont1

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.


3 in class exercise on interface design

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).


  • Login