Upload
yamal
View
47
Download
0
Tags:
Embed Size (px)
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
Citation preview
Chapter 6:Modular Programming
By: Suraya Alias
1-1
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
Figure 6.1 Function separate
1-3
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
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
Figure 6.3 Program That Calls a Function with Output Arguments
1-6
Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)
1-7
Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)
1-8
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
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
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
Figure 6.6 Program to Sort Three Numbers
1-12
Figure 6.6 Program to Sort Three Numbers (cont’d)
1-13
Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);
1-14
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
6.3 Scope of NamesScope of name – the region in a
program where a particular meaning of a name is visible
1-16
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
Figure 6.8 Outline of Program for Studying Scope of Names
1-18
Figure 6.9 Function scan_fraction (incomplete)
1-19
Figure 6.9 Function scan_fraction (incomplete) (cont’d)
1-20
Figure 6.10 Data Areas for scan_fraction and Its Caller
1-21
Figure 6.11 Structure Chart for Common Fraction Problem
1-22
6.5 A program with multiple function
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
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions
1-24
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
1-25
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
1-26
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
1-27
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
1-28
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
1-29
Figure 6.13 Sample Run of a Partially Complete Program Containing Stubs
1-30
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
Figure 6.14 Stub for Function multiply_fractions
1-32
Figure 6.15 Driver for Function scan_fraction
1-33
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.