Chapter 6: Modular Programming By: Suraya Alias

34
Chapter 6: Modular Programming By: Suraya Alias 1-1

description

Chapter 6: Modular Programming By: Suraya Alias. 6.1 Functions with simple output parameters. Argument list provide the communication links between the main function and its function subprograms. We can use output parameter to return multiple results from a function - PowerPoint PPT Presentation

Transcript of Chapter 6: Modular Programming By: Suraya Alias

Page 1: Chapter 6: Modular Programming By:  Suraya  Alias

Chapter 6:Modular Programming

By: Suraya Alias

1-1

Page 2: Chapter 6: Modular Programming By:  Suraya  Alias

6.1 Functions with simple output parameters

Argument list provide the communication links between the main function and its function subprograms.

We can use output parameter to return multiple results from a function

A function can send back multiple output to the function that calls it

1-2

Page 3: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.1 Function separate

1-3

Page 4: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.2 Diagram of Function separate with Multiple Results

void separate(double num, char *signp, int *wholep, double *fracp)

Explanation:

double num /* input - value to be split*/

char *signp, /* output - sign of num */

int *wholep, /* output - whole number magnitude of num */

double *fracp) /* output - fractional part of num */

This function is void as it does not return any results, the function body also does not include return statement.

1-4

Page 5: Chapter 6: Modular Programming By:  Suraya  Alias

PointerPointer is a memory cell whose content

is the address of another memory cellA declaration to a parameter such as

char *signp tells the compiler that signp contain the address of a type char variable

So, parameter signp is a pointer to type char variable,

The name signp, comes from sign and pointer thus signp

1-5

Page 6: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.3 Program That Calls a Function with Output Arguments

1-6

Page 7: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)

1-7

Page 8: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)

1-8

Page 9: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.4 Parameter Correspondence for separate(value, &sn, &whl, &fr);

1-9

• Address-of–operator (&) on the actual argument sn, whl and fr is compulsory

Page 10: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.5 Comparison of Direct and Indirect Reference

1-10

• In front of each parameter is the indirect operator, unary *• When * applied it has the effect of following the pointer referenced by its pointer

Page 11: Chapter 6: Modular Programming By:  Suraya  Alias

6.2 Multiple Calls to a function with input output parameter

This example demonstrates the use of single parameter to carry value and also result out of a function

Also, how a function may be called more than once

Sort – a rearrangement of data in a particular sequence (either decreasing or increasing)

1-11

Page 12: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.6 Program to Sort Three Numbers

1-12

Page 13: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.6 Program to Sort Three Numbers (cont’d)

1-13

Page 14: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);

1-14

Page 15: Chapter 6: Modular Programming By:  Suraya  Alias

Trace of program sort

1-15

Statement Num1 Num2 Num3 effect

Scanf(…, &num1,&num2,&num3)

7.5 9.6 5.5 Enters data

Order(&num1, &num2); No change

Order(&num1,&num3); 5.5 9.6 7.5 Switches num1 and num3

Order(&num2, &num3) 5,5 7.5 9.6 Switches num2 and num3

Printf(…, num1,num2,num3)

Displays 5.5, 7.5,9.6

Page 16: Chapter 6: Modular Programming By:  Suraya  Alias

6.3 Scope of NamesScope of name – the region in a

program where a particular meaning of a name is visible

1-16

Page 17: Chapter 6: Modular Programming By:  Suraya  Alias

6.4 Formal output parameters as actual arguments

Sometimes a function needs to pass its own output parameter as an argument when it calls another function.

In the example, because nump and denomp store addresses, it can be used directly in the call to scanf:

scanf(“%d%c%d”, nump &slash,denomp);Scanf will store the first number scanned in the

variable whose address is in the nump, next the slash in local variable slash and the scanned number in the variable whose address is in denomp.

1-17

Page 18: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.8 Outline of Program for Studying Scope of Names

1-18

Page 19: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.9 Function scan_fraction (incomplete)

1-19

Page 20: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.9 Function scan_fraction (incomplete) (cont’d)

1-20

Page 21: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.10 Data Areas for scan_fraction and Its Caller

1-21

Page 22: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.11 Structure Chart for Common Fraction Problem

1-22

6.5 A program with multiple function

Page 23: Chapter 6: Modular Programming By:  Suraya  Alias

ImplementationStub

◦A skeleton function that consists of a header and statements that display trace messages and assign values to output parameters

◦It enables testing of the flow of control among functions before the function is completed.

1-23

Page 24: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions

1-24

Page 25: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

1-25

Page 26: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

1-26

Page 27: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

1-27

Page 28: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

1-28

Page 29: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)

1-29

Page 30: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.13 Sample Run of a Partially Complete Program Containing Stubs

1-30

Page 31: Chapter 6: Modular Programming By:  Suraya  Alias

6.6 Debugging and Testing a Program System1. Top-down testing

◦ The process of testing flow of control between a main function and its subordinate functions

2. Unit test◦ A test of an individual function

3. Bottom-up testing◦ The process of separately testing individual

functions of a program system

4. System Integration testing◦ Testing a system after replacing all its stubs

with functions that have been pretested1-31

Page 32: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.14 Stub for Function multiply_fractions

1-32

Page 33: Chapter 6: Modular Programming By:  Suraya  Alias

Figure 6.15 Driver for Function scan_fraction

1-33

Page 34: Chapter 6: Modular Programming By:  Suraya  Alias

1-34

Function that returns multiple result

Effect and sample call

Void make_change (double change, double token_val, int * num_tokenp, double *leftp){ *num_tokenp = floor(change /token_val); *leftp = change - *num_tokenp * token_val;

}

Determines how many of a certain bill or coin (token_val) should be included in change amount.This number is sent back through the output parameter num_tokenp.The amount of change is sent back through the output parameter leftp.