340 likes | 466 Views
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
E N D
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
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.
Pointer • Pointer is a memory cell whose content is the address of another memory cell • A declaration to a parameter such as char *signp tells the compiler that signpcontain 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
Figure 6.3 Program That Calls a Function with Output Arguments
Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)
Figure 6.3 Program That Calls a Function with Output Arguments (cont’d)
Figure 6.4 Parameter Correspondence for separate(value, &sn, &whl, &fr); • Address-of–operator (&) on the actual argument sn, whl and fr is compulsory
Figure 6.5 Comparison of Direct and Indirect Reference • 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)
Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);
6.3 Scope of Names • Scope of name – the region in a program where a particular meaning of a name is visible
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.
6.5 A program with multiple function Figure 6.11 Structure Chart for Common Fraction Problem
Implementation • Stub • 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.
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.12 Program to Perform Arithmetic Operations on Common Fractions (cont’d)
Figure 6.13 Sample Run of a Partially Complete Program Containing Stubs
6.6 Debugging and Testing a Program System • Top-down testing • The process of testing flow of control between a main function and its subordinate functions • Unit test • A test of an individual function • Bottom-up testing • The process of separately testing individual functions of a program system • System Integration testing • Testing a system after replacing all its stubs with functions that have been pretested