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

  • View
    218

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

Chapter 6

Modular Programming andFunctions

Page 2: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

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.

Page 3: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 4: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 5: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

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

• 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.

Page 7: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 8: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

Main_program

Print_menu Draw_rectangle Draw_triangle

Page 9: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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

Page 10: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 6.3 ELEMENTS OF MODULAR PROGRAMS• C requires that function names be unique in a sour

ce program file.• Program execution always begins and eventually t

erminates 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.

Page 11: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

void pront_menu (void){

printf(“THIS PROGRAM DRAWS A RECTANGLE OR A TRI

ANGLE ON THE “);

printf(“SCREEN. \n”);

printf(“Enter 1 to draw a rectangle. \n”);

printf(“Enter 2 to draw a triangle: “);

} /* end function print_menu */

Page 12: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 13: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 14: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 6.4 STRUCTURE OF MODULAR PROGRAMS• In structure charts, different modules invoked by t

he same module are shown on the same level.• Modules on the same level of hierarchy are unders

tood 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.

Page 15: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 16: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

Main_program

Print_menu

Draw_triangleDraw_rectangleUser_request

request

Page 17: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

int user_request(void)

we must place at least one return statement somewhere in its body.

return request;

Page 18: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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)

Page 19: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

Main_program

Draw_triangleDraw_rectangle

Process_request

Print_menu

User_request

Request_codeRequest_code

void process_request(int request_code)

Page 20: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 6.8 FUNCTIONS WITH PARAMETERS• The are three ways through which functions can c

ommunicate 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.

Page 21: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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

Page 22: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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

Page 23: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 24: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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);

Page 25: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 6.10 STORAGE CLASSES, SCOPE, VISIBILITY, AND DURATION OF VARIABLES– The region in the program in which it can be us

ed legitimately, that is, its scope.– A program’s ability to access that variable’s me

mory location, that is, its visibility.– The time during which a memory location exist

s for that variable, that is, its duration.– Where it is located in the main memory at run ti

me.

Page 26: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 27: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

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 */

Page 28: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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.

Page 29: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 6.11 EXAMPLE PROGRAM 4:

Main_program

Print_status

Computed_taxPrint_status_menu

Output_resultsEntered_filing_statusEntered_gross_income

income status Incomestatus tax Income

Statustax

status

Page 30: Chapter 6 Modular Programming and Functions. 6.1 INTRODUCTION Several programmers work together in teams on the same project. In addition, there is the

• 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