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

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Spring Semester 2013 Lecture 5
Modular Programming With Functions
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
C++ for Engineers and Scientists Third Edition
chap13 Chapter 13 Programming in the Large.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 ICS103 Programming in C Lecture 8: Functions I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 7: User-Defined Functions II
The Three Attributes of an Identifier
CSCI 161: Introduction to Programming Function
Programmazione I a.a. 2017/2018.
User-Defined Functions
Chapter 4 void Functions
6 Chapter Functions.
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
Topics Introduction to Functions Defining and Calling a Function
Classes, Objects and Methods
Presentation transcript:

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

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.

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.

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.

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.

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.

Main_program Print_menu Draw_rectangleDraw_triangle

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

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.

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

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.

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.

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.

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.

Main_program Print_menu Draw_triangle Draw_rectangleUser_request request

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

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)

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

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.

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

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

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.

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

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.

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.

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

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.

6.11 EXAMPLE PROGRAM 4: Main_program Print_status Computed_tax Print_status_menu Output_results Entered_filing_statusEntered_gross_income income status Income status tax Income Status tax status

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