1 of 42

CNSP - Lecture 6 Chapter 6�Part II

BY PROF. RAFAEL ORTA

Disclosure: this presentation includes slides that were provided by the textbook publisher and Dr. Hnatyshin, some are adaptation and some are an exact replica.

2 of 42

This week we will cover

  • Review about how to return function results through function arguments
  • Differences between call-by-value and call-by-reference
  • Understand difference between input, input / output, and output parameters.
  • How to modularize programs system and pass information between system modules.
  • How to document the flow of information using structure charts.
  • Learn testing and debugging techniques appropriate for a program system with several modules.

3 of 42

What are the 3 repetition flow control structures we covered last week?

4 of 42

While, For and Do- While

5 of 42

6.1 Value and Reference Parameters

  • Functions can
    • return no value
      • type void
    • return exactly one value
      • return statement
      • function type
    • return more than one value
      • type void
      • reference parameters

5

6 of 42

Listing 6.1 Function to compute sum and average

6

7 of 42

Notes on Call-by-Reference

  • Place the & also in the prototype:

void computeSumAve(float, float, float&, float&);

  • Note that this is a void function

7

8 of 42

Call-by-Value and Call-by-Reference Parameters

  • & between type and identifier defines a parameter as output mode (pass by reference)
  • no & in a parameter’s declaration identifies input mode (pass by value)
  • Compiler uses info in parameter declaration list to set up correct argument-passing mechanism

8

9 of 42

When to Use a Reference or a Value Parameter

  • If information is to be passed into a function and doesn’t have to be returned or passed out of the function, then the formal parameter representing that information should be a value parameter (input parameter)
  • If information is to be returned to the calling function through a parameter, then the formal parameter representing that information must be a reference parameter (output parameter).
  • If information is to be passed into a function, perhaps modified, and a new value returned, then the formal parameter representing that information must be a reference parameter (input/output parameter)
  • Note: C++ does not distinguish between output parameters and input/output parameters

10 of 42

Program Style

  • Formal parameter list often written on multiple lines to improve readability.
  • Value parameters first, followed by reference parameters
  • Comment each parameter to identify its use

10

11 of 42

Summary of Value and Reference Parameters

11

12 of 42

Argument/Parameter List Correspondence

  • Number: number of actual arguments used in function call must be the same as the number of formal parameters listed in the function header and prototype
  • Order: Order of arguments in the lists determines correspondence
  • Type: Each actual argument must be of a data type that can be assigned to the corresponding formal parameter with no unexpected loss of information
  • For reference parameters, an actual argument must be a variable. For value parameters, an actual argument may be a variable, a constant, or an expression

12

13 of 42

How do you declare a reference parameter?

14 of 42

A/ You place an & after the variable type.

15 of 42

6.2 Functions with Output and Input Parameters

  • Function getFrac reads a common fraction typed in by a user and returns the numerator and denominator through two type int output parameters.

15

16 of 42

Listing 6.5 Function to order three numbers

16

Notice that the output of one function is the input of the other

17 of 42

Does C++ makes any distinction between output and input – output parameters?

18 of 42

A/ No

19 of 42

6.3 Stepwise Design with Functions

  • Arguments are used to pass information to and from functions
  • Use functions to encode complex tasks

19

20 of 42

Figure 6.4 Structure chart for general sum and average problem

20

21 of 42

What is an alternative to returning values from a function other than passing arguments by reference in the function call?

22 of 42

A/ To use the return clause of the function.

23 of 42

In a function where you are passing to it all the parameters by value, can you return the output or result of it without using the return clause or global variables?

24 of 42

A/ No, it must be either passed by reference, using a global variable, or using the return clause.

25 of 42

6.5 Debugging and Testing a Program System

  • Keep each function to a manageable size
    • errors less likely
    • easier to read
    • simplifies testing
  • Two kinds of testing
    • Top-down
    • Bottom-up

25

Why?

26 of 42

Top-Down Testing and Stubs

  • Useful for large projects
  • Stubs for all functions not finished (substitute for a specific function) - just a heading without any details other than some type of message

26

27 of 42

Listing 6.12 Stub for function computeSum

27

28 of 42

Bottom-Up Testing and Drivers

  • Unit testing tests each function individually
  • Driver used by developer to test full functionality of their function
    • contains only sufficient declarations and executable statements to test a specific function
  • System integration tests combine functions for additional testing, in other words testing of the entire system.

28

29 of 42

Listing 6.13 A driver to test computeSum

29

30 of 42

Debugging Tips for Program Systems

  • Use comments to
    • document each function parameter and local variable
    • describe the function’s purpose
  • Create a trace of execution by outputting the function name as the function is entered
  • Trace (display) the values of all input and inout parameters upon function entry.
  • Trace the values of all function outputs after returning from function. Verify. Make sure all inout and output parameters are declared as reference parameters (&),
  • Make sure function stub assigns a value to each output parameter.
  • Make sure function driver assigns a value to each input parameter

30

31 of 42

Debugging Tips for Program Systems

31

32 of 42

Black-Box Testing

  • Assumes the program tester has no information about the code inside the function or system.
  • Tester’s job is to
    • verify that the function or system meets specifications.
    • for each function, ensure that post-conditions are satisfied whenever its preconditions are met
    • check for function crashing due to invalid input values
    • check boundaries of system

32

33 of 42

White-Box Testing

  • Tester has full knowledge of function code
  • Must ensure each section of code has been thoroughly tested.
    • Check all possible paths
    • Determine that for each test correct path is taken
    • For loops, make sure correct number of iterations
    • Check boundary values

33

34 of 42

What is the difference between black-box testing and white-box testing?

35 of 42

A/ Black-box implies no knowledge about the program by the tested, white-box has knowledge and focuses in testing all possible paths?

36 of 42

37 of 42

6.6 Recursive Functions

  • Recursive function calls itself, with different argument values for each call
  • Must identify a stopping case - a situation that stops the recursion.

37

38 of 42

Template for a Recursive Function

1. If the stopping case is reached

1.1 Return a value for the stopping case

Else

1.2 Return a value computed by calling the function again with different arguments

38

39 of 42

Listing 6.14 Recursive function factorial

39

40 of 42

41 of 42

6.7 Common Programming Errors

  • Parameter inconsistencies with call-by-reference parameters
  • Forgetting to use & with call-by-reference parameters
  • Warning messages from compiler should not be ignored

41

42 of 42