Modular Programming - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Modular Programming

Description:

Straight Progression sometimes called spaghetti code. Modular format each module has one activity. ... Modular programming is the format used in advanced ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 17
Provided by: scie228
Category:

less

Transcript and Presenter's Notes

Title: Modular Programming


1
Modular Programming
  • Programs are usually viewed in two formats
  • Straight Progression sometimes called spaghetti
    code
  • Modular format each module has one activity.
  • Modular programming is the format used in
    advanced programming languages.
  • Most modern programming languages have two types
    of modules
  • Procedures no value needs to be returned.
    Example PRINTING, SORTING
  • Functions some value is returned. Example
    calculations, data input, file operation.

2
Modular Programming
100 ' Demo on Arrays. 110 ' demo difference
between single variable and array 200 ' Variable
List 210 ' ar(r,1) - product name 220 '
ar(r,1) - price 230 ' ar(r,2) - quantity 240 '
ar(r,3) - cost 250 ' tc - total cost 300 '
Initialization
310 R 10 320 SFLD 1 330 NFLD
3 340 ' 350 CNT 0 360 FLG "Y" 370 HD "
Product Quantity Price Cost" 380
T " \ \ ,.
,." 390 ' 400 ' Create
Array Space 410 DIM AR(R,SFLD) 420
DIM AR(R,NFLD) 430 '
440 '
3
Modular Programming
500 ' Main Body
510 ' Data Input Module 520
WHILE (FLG "Y") 530 CNT CNT 1 540 FLG
"n" 550 COLOR 11,0 CLS 560 LOCATE 5,10
INPUT "Enter Product Name " AR(CNT,1) 565 COLOR
15,4 570 LOCATE 22,8 INPUT "Do this again "
FLG 580 IF (FLG "y") OR (FLG "") THEN FLG
"Y" 590 WEND 600 COLOR 14,0 CLS 610 PRINT
USING "you have entered products " CNT 620
INPUT "hit enter to continue" Q 650 '
Purchase Module 660 FOR
J 1 TO CNT 670 CLS 680 LOCATE 5,8 PRINT
"Product - " AR(J,1) 690 LOCATE 7,8 INPUT
"How many would you like to purchase "
AR(J,1) 700 LOCATE 9,8 INPUT "What is the price
of each one " AR(J,2) 710 NEXT J
4
Modular Programming
720 ' Calculate Module
730 FOR J 1 TO CNT 740 CLS 750
AR(J,3) AR(J,1) AR(J,2) ' calculate
cost 760 TC TC AR(J,3) '
calculate total cost 770 NEXT J 800 '
Print Module 810
COLOR 14,1 CLS 820 PRINT HD 830 FOR J 1 TO
CNT 840 PRINT USING T AR(J,1), AR(J,1),
AR(J,2), AR(J,3) 850 NEXT J 860 PRINT USING
"Total cost ,." TC 999 END
5
Modular Programming
Why should we program using a modular approach?
  • Each module does one activity
  • Modules can be used in different programs
  • Modules can be copied from one program to
    another, eliminating the need to recreate code
    that works.
  • Modules allow for a consistency from one program
    to another for standard activities.

6
Modular Programming
Each task is a module or procedure. In
modular programming, each module can be written
independently.
Start Program
Initialize Module
GET DATA Module
Calculate Cost Module
GET Purchase Module
Print Module
END
7
Modular Programming
Each task is a module or procedure. In
modular programming, each module can be written
independently.
Start Program
Get Data Module
Initialize Module
Loop
GET DATA Module
Counter
Calculate Cost Module
Data Input
GET Purchase Module
Do Again
Print Module
End Loop
END
8
Modular Programming
In BASIC, we do not distinguish between
PROCEDURES and FUNCTIONS. Modules in BASIC are
written as SUBROUTINES.
  • In modular programming, there are two categories
    of code sets
  • MAIN BODY that part of the program that acts as
    a control center for the execution of the modules
  • SUBROUTINES that part of the program that
    contains the algorithms for different operations.

SUBROUTINES are located after the END statement.
9
Modular Programming
Each task is a module or procedure. In
modular programming, each module is written
independently, and the order of the modules is
not relevant.
The order in which the main body calls each
module is relevant.
Rules Each module is written as a SUBROUTINE.
Each SUBROUTINE must be after the END statement!
The main program will call a SUBROUTINE for a
specific activity. A SUBROUTINE can call another
SUBROUTINE, but it cannot call itself.
10
Modular Programming
Start Program
Initialize Subroutine
GET DATA Subroutine
Calculate Cost Subroutine
GET Purchase Subroutine
Print Subroutine
Sort Subroutine
Print Subroutine
END
Calculate Cost
Initialize values
Get Data A, B, C
Print Routine
Get Purchase
11
Modular Programming GOSUB/RETURN
The SUBROUTINE command set is GOSUB / RETURN
GOSUB send the program to the designated
subroutine. BASIC remembers the line which
sent it to the subroutine.
RETURN returns the program to the line which
initiated the subroutine. The RETURN command also
marks the end of the individual subroutine.
12
Modular Programming
Example 200 GOSUB 1000 Initialize
values 210 WHILE (FLG Y) . . .
800 WEND 999 END 1000 Initialize
values 1010 FLG Y 1020 CNT 0 1030
TOTAL 0 1090 RETURN
13
Modular Programming
  • For this course, there are some standards in
    writing a subroutine
  • Each subroutine begins with a comment line,
    identifying what is accomplished in the
    subroutine
  • Each subroutine starts on a xx00 line.
  • Each subroutine ends with the RETURN on a line
    xx90.
  • Each subroutine does one function.
  • Example
  • 3000 Do again routine
  • 3010 PRINT Do you want to do this again Y / N
  • 3030 INPUT FLG
  • IF (FLG y) OR FLG )THEN FLG Y
  • 3090 RETURN

14
Modular Programming
  • Why Subroutine programming
  • Subroutines can be reused in other programs.
    They do not have to be rewritten.
  • Subroutines can be written in any order. The
    only ordering is when the program is assembled
    for execution.
  • Subroutines allow for program testing of standard
    routines (input, output, etc.) before complex
    routines are written.
  • Subroutines can be used multiple times.
  • Subroutines are required for sorting data.

15
Modular Programming
Modular Programming allows the programmer
flexibility in designing a program, and in
writing a program. Modular Programming reduces
redundant coding, by allowing the routine to be
called from different parts of the
program. Modular Programming allows for standard
routines to be copied from a library.
16
Modular Programming
Some guidelines for modular construction Block
1000 1100 Initialization Block 1500 1800
File operation Block 2000 2500 Sorting Block
3000 3500 Program Control Block 3600 to any
block number program activity
Write a Comment
User Comments (0)
About PowerShow.com