# Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.

## Presentation on theme: "Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University."— Presentation transcript:

Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-2 Functions with Simple Output Parameters When a function call executes, the computer allocates memory space in the function data area for each formal parameter. The value of each actual parameter is stored in the memory cell allocated to its corresponding formal parameter.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-5 Functions with Simple Output Parameters (Cont’d) Asterisks –before the parameter names –in front of the parameter names in the assignment statements char *signp tells the compiler that signp will contain the address of a type char variable. pointer –a memory cell whose content is the address of another memory cell

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-8 Functions with Simple Output Parameters (Cont’d) The calling function must declare variables in which function separate can store the multiple results it computes. Changing of the values of memory cells in the data area of the calling function is considered a side effect of the call to function separate. The addresses of the arguments sn, whl, and fr are stored in the corresponding output parameters signp, wholep, and fracp.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-12 Functions with Simple Output Parameters (Cont’d) The formal parameter is preceded by the indirection operator, unary *. –*signp = '+'; –*wholep = floor(magnitude); –*fracp = magnitude - *wholep;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-13 Meanings of * Symbol Three distinct meanings of the symbol * –the binary operator meaning multiplication. –in the declarations, read as “pointer to” –the unary indirection operator, “follow the pointer”

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-16 Multiple Calls to a Function with Input/Output Parameters sort –a rearrangement of data in a particular sequence (increasing or decreasing) input/output parameters –the function uses the current actual argument values as inputs and may return new values

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-19 Scope of Names scope of a name –the region in a program where a particular meaning of a name is visible All of the formal parameters and local variables are visible only from their declaration to the closing brace in which they are declared.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-25 CASE STUDY Arithmetic with Common Fractions To perform computations with common fractions and get results that are common fractions in reduced form Add, subtract, multiply, and divide several pairs of common fractions.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-26 CASE STUDY Arithmetic with Common Fractions (Cont’d) DATA REQUIREMENTS –Problem Inputs int n1, d1 /* numerator, denominator of first fraction */ int n2, d2 /* numerator, denominator of second fraction */ char op /* arithmetic operator + - * or / */ char again /* y or n depending on user's desire to continue */ –Problem Outputs int n_ans /* numerator of answer */ int d_ans /* denominator of answer */

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-27 CASE STUDY Arithmetic with Common Fractions (Cont’d) INITIAL ALGORITHM –1. Repeat as long as user wants to continue. –2. Get a fraction problem. –3. Compute the result. –4. Display problem and result. –5. Check if user wants to continue. Step 2 Refinement –2.1 Get first fraction. –2.2 Get operator. –2.3 Get second fraction.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-28 CASE STUDY Arithmetic with Common Fractions (Cont’d) Step 3 Refinement –3.1 Select a task based on operator: –'+': 3.1.2 Add the fractions. –‘-': 3.1.3 Add the first fraction and the negation of the second. –'*': 3.1.4 Multiply the fractions. –'/': 3.1.5 Multiply the first fraction and the reciprocal of the second. –3.2 Put the result fraction in reduced form. Step 3.2 Refinement –3.2.1 Find the greatest common divisor (GCD) of the numerator and denominator. –3.2.2 Divide the numerator and denominator by the GCD.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-37 Debugging and Testing a Program System Not all functions will be ready at the same time use of stubs enables us to test and debug the main program flow and those functions that are available top-down testing –the process of testing flow of control between a main function and its subordinate functions

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-38 Debugging and Testing a Program System (Cont’d) stub –a skeleton function that consists of a header and statements that display trace messages and assign values to output parameters; enables testing of the flow of control among functions before this function is completed

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-40 Debugging and Testing a Program System (Cont’d) unit test –a test of an individual function –Perform a preliminary test of a new function –It is easier to locate and correct errors when dealing with a single function rather than with a complete program system. Perform a unit test by writing a short driver function to call it. Once a function works properly, substitute it for its stub

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-42 Debugging and Testing a Program System (Cont’d) bottom-up testing –the process of separately testing individual functions of a program system system integration tests –testing a system after replacing all its stubs with functions that have been pretested

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-43 Debugging Tips for Program Systems A list of suggestions for debugging a program system follows. –Carefully document each function parameter and local variable using comments –Create a trace of execution by displaying the function name as you enter it –Trace or display the values of parameters upon entry to a function –Trace or display the values of all function outputs

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6-44 Common Programming Errors An actual output argument must be of the same pointer data type as the corresponding formal parameter. The C scope rules

Download ppt "Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University."

Similar presentations