1 / 30

Chapter 6 Modular Programming and Functions

Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION. Several programmers work together in teams on the same project. In addition, there is the problem of software maintenance.

lucillen
Download Presentation

Chapter 6 Modular Programming and Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6Modular Programming andFunctions

  2. 6.1 INTRODUCTION • Several programmers work together in teams on the same project. • In addition, there is the problem of software maintenance. • In general, a software system is a just another complex artificial system. In developing it we use a similar strategy, namely, the divide-and-conquer approach.

  3. Modular programming is defined as organizing a program into small, independent modules that are separately named and individually invokeable program elements. These modules are integrated to become a software system that satisfies the problem requirements.

  4. 6.2 EXAMPLE PROGRAM 1: A Modular C Program that Draws Geometrical Figures (Use of Functions that Do Not Receive or Send Data • We will assign a name to each module and combine the named modules in a program structure under the control of a main program. Such a program structure consists of a set of modules and an order of execution.

  5. This strategy is essentially based on the divide-and-conquer approach to problem solving and has many advantages over developing a monolithic program for the entire problem. • 1. When we modularize a problem, we end up with a set of smaller and simpler problems, which can be assigned to different teams. • 2. Converting simple problems to individual programs facilitates programming, program debugging, testing, expansion, repair, and maintenance.

  6. 3. We can change the modules easily because they are relatively small. Thus modular programs improve program portability. • 4. Some general-purpose modules of a modular program can be used without any change in other software development projects. • Now we have a main program and three subprograms. Together, they make up a modular program.

  7. The main program appears at the top of the chart. • A line connecting a module to a module below it means that the top module calls the one that is at a lower level of hierarchy. Also, that the modules print_menu, draw_rectangle, and draw_triangle are drawn in this order, from left to right, means that the main program calls them in this order.

  8. Main_program Print_menu Draw_rectangle Draw_triangle

  9. If there is only one programmer, he or she may develop the entire program, one module at a time, starting with the modules at the lowest level of hierarchy in the structure chart of Figure 6.3 and moving up from there. This implementation strategy is referred to as bottom-up implementation. • These functions require three elements: • 1. Function definitions • 2. Function calls • 3. Function declarations

  10. 6.3 ELEMENTS OF MODULAR PROGRAMS • C requires that function names be unique in a source program file. • Program execution always begins and eventually terminates in the main function. • A function definition consists of • 1. A function type • 2. A function name • 3. An optional list of formal parameters enclosed in parentheses • 4. A compound statement.

  11. void pront_menu (void){ printf(“THIS PROGRAM DRAWS A RECTANGLE OR A TRIANGLE ON THE “); printf(“SCREEN. \n”); printf(“Enter 1 to draw a rectangle. \n”); printf(“Enter 2 to draw a triangle: “); } /* end function print_menu */

  12. To indicate that the formal parameter list is empty, we use the keyword void between parentheses • Function calls • A function call requires the name of the function followed by a list of actual parameters, if any, • When a function call is encountered, the program control passes to the called function. • After the function body completes its execution, the program control goes back to the calling function.

  13. Function declarations • Void print_menu(void); • Before a function can be called, it must be either defined or declared by a prototype in the source file. • We will place function prototypes right after the preprocessor directives and before the definition of function main.

  14. 6.4 STRUCTURE OF MODULAR PROGRAMS • In structure charts, different modules invoked by the same module are shown on the same level. • Modules on the same level of hierarchy are understood to be executed in left-to-right order. • It is possible to convert a nontree structure chart to an equivalent tree structure chart by repeating the modules that are used by more than one module. Figure 6.7 shows a tree structure chart that is equivalent to the structure of Figure 6.6.

  15. To show that a module invocation is conditional, we draw a small diamond-shaped block on the bottom edge of the box for the calling module. • Repetitive module invocations are shown by drawing a clockwise arc over the line connecting the modules.

  16. Main_program request Draw_triangle User_request Draw_rectangle Print_menu

  17. int user_request(void) we must place at least one return statement somewhere in its body. return request;

  18. 6.6 FUNCTIONS THAT RETURN VALUES UNDER THEIR NAMES • Return statement terminates the execution of the function and takes the program control back to the calling program. • 6.7 EXAMPLE PROGRAM 3: A Modular C Program that Draws Geometrical Figures (Use of Functions that Receive Data Values)

  19. Main_program Request_code Request_code Process_request User_request Print_menu Draw_rectangle Draw_triangle void process_request(int request_code)

  20. 6.8 FUNCTIONS WITH PARAMETERS • The are three ways through which functions can communicate data: • Through global variables, which are variables declared in the source file outside and before function definitions • By returning a value under a function name • By using parameters • All function in a source file can access global variables. Therefore, we can use global variables for passing values to a function and returning values from it.

  21. However, using global variables for data communication purposes has certain draw backs • If they are allowed to access global variables, this independence is largely compromised. • A function may have any number of parameters. • C supports two types of formal parameters: • Value parameters • Pointer parameters

  22. Calling functions with parameters • Actual parameters (or arguments) are constants, variables, or expressions in a function call that correspond to its formal parameters. • Correspondence of Actual and Formal Parameters • The number of actual parameters in a function call must be the same as the number of formal parameters in the function definition. • A one-to-one correspondence must occur among the actual and formal parameters. • The type of each actual parameter must be either the same as that of the corresponding formal parameter

  23. 6.9 PARAMETER PASSING BY VALUE • During this process, the values of the actual parameters are copied to the memory locations of the corresponding formal parameters in the called function’s data area. • It also means that we can use value parameters only to send values to functions, and not to receive values from them.

  24. Passing Strings to Functions Through Parameters • Suppose we have a string variable declared char student_name[31] Void print_name(char stu_name[]) { printf(“%s” , stu_name); } /* end function print_name */ The prototype for this function can be void print_name(char stu_name[]) And a typical function call is print_name(student_name);

  25. 6.10 STORAGE CLASSES, SCOPE, VISIBILITY, AND DURATION OF VARIABLES • The region in the program in which it can be used legitimately, that is, its scope. • A program’s ability to access that variable’s memory location, that is, its visibility. • The time during which a memory location exists for that variable, that is, its duration. • Where it is located in the main memory at run time.

  26. Variables of storage class static are allocated memory locations that exist during the execution of the program in which they are declared. On the other hand, automatic variables are reserved memory locations that exist only during the execution of the function in which they are declared. All global variables are static, and all local variables and function formal parameters are automatic by default.

  27. void draw_rectangle(void) { static int no_of_execs = 0; printf(“+----------+\n”); printf(“| |\n”); printf(“| |\n”); printf(“+----------+\n”); no_of_execs++; printf(“This is the %dth rectangle drawn. \n , no_of_execs); } /* end function draw_rectangle */

  28. Scope of Variables • The scope of a variable is the region in which it can be used legitimately. • The scope of any variable begins at the point of its declaration • A global variable’s scope begins at the point of its declaration and terminates at the end of the file • Formal function parameters are local to the function in which they are declared. The scope of formal function parameters is the same as the scope of local variables, beginning at the point where they are declared and extending until the end of the function.

  29. Main_program • 6.11 EXAMPLE PROGRAM 4: Income status status Income Status tax income tax Entered_gross_income Entered_filing_status Output_results Computed_tax Print_status_menu status Print_status

  30. Implementing a Modular Program in C Using Bottom-Up Development • 6.12 USING DRIVER FUNCTIONS TO TEST MODULES • A test driver is a C program that calls on the module whose development is completed to verify that it does what ir is expected to do properly

More Related