0% found this document useful (0 votes)
1 views56 pages

Modular Programming PDF

The document discusses modular programming, highlighting its advantages over linear programming, such as improved readability, reusability, and easier maintenance. It explains the structure of modular programs, including main and sub-modules, and details the creation and usage of sub procedures in QBASIC. Additionally, it covers argument passing methods, local and global variables, and function procedures, providing examples and syntax for each concept.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views56 pages

Modular Programming PDF

The document discusses modular programming, highlighting its advantages over linear programming, such as improved readability, reusability, and easier maintenance. It explains the structure of modular programs, including main and sub-modules, and details the creation and usage of sub procedures in QBASIC. Additionally, it covers argument passing methods, local and global variables, and function procedures, providing examples and syntax for each concept.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULAR PROGRAMMING

Difference between linear and modular programming

Aspects Linear Programming Modular Programming

Structure Single, continuous sequence of Divide into separate modules or


instructions functions
Readability Become difficult to read and Easier to read, understand and
maintain as it grows. maintain.
Reusability Low reusability, code is specific to a High reusability, modules can be
single task reused across different programs.
Maintenance Difficult to maintain and debug Easier to maintain and debug.

Scalability Changes can affect the entire New models can be added without
program. affecting existing ones.
Testing It is more challenging as the whole Testing is easier, individual modules
program must be tested. can be tested independently.
Difference between F5 and SHIFT+F5
• F5: Simply runs the program from the start.
• SHIFT + F5: Restarts the program if it’s already running, stopping it first and then
starting it again from the beginning.

• So, if you're coding in QBasic and you want to run your program, you can press F5.

• If your program is already running and you want to restart it, you can use SHIFT +
F5. This helps you quickly see the effects of any changes you make to your code.
Introduction
• It is the programming technique in which a large and complex
program is divided into small logical and manageable part of the
program which can perform specific task
• The small, logical and manageable part of the program is called
procedure (module).
• Modular programming uses a small block of functional codes, it is
also called structured programming.
• Every modular program has one main module and one or more sub
modules (procedure)
Main
Module

Sub Sub Sub


module A module B module C

Sub
module B1

Sub
module B2
Advantages of Modular programming
• A procedure can be reused in a program which reduces the length of the program.

• It is suitable for team work so that tasks can be divided with a team member.

• The debugging of the program becomes easier and faster since they are divided into
different modules which can be tested independently.

• The procedure can be tested and debugged separately.

• The documentation of an individual procedure is simpler as compared to the


documentation of a large and complex program.
Parts of modular programming
Program is divided into two parts:
[Link] Module
• It is the top level module which is located at the top of a procedure.
• It is the entry point of modular programming .
• Procedure names and parameters are declared and called in the main module.
[Link] Module(procedure)
• It is a program which is written under the main module.
• It may have one or more than one modules under main module.
• A sub module may have other sub module.
Types of procedure
QBASIC supports two types of modular programming
• FUNCTION procedure
• SUB procedure
SUB procedure:
• A Sub procedure is a small, logical and manageable functional
part of a program which performs the specific tasks and does
not return any value to the main module.
Features of SUB Procedure
• It is defined by unique name
• It does not return any value
• Name of sub procedure can not be used as a variable name
• It is declared in the top of the main module
• A SUB program may call another SUB program
Creating SUB procedure
To create sub procedure, we must follow three processes:
Declare Procedure
Define Sub Procedure
CALL Sub Procedure
Declaring a SUB procedure
Function: This statement is used to declare SUB procedure. It is non
executable statement.
Syntax: DECLARE SUB <procedure name>[(parameter list)]
Where, parameter list is the variables that receives values and are
passed to SUB procedure when it is called
Example: DECLARE SUB area(a,b)
Here, area is the procedure name and a,b are the parameters
Defining SUB procedure
Function: SUB statement is used to define a sub procedure.
Syntax: SUB <procedure name>[(parameter list)]
statement block
END SUB

Example: SUB area(a,b)


ar=a*b
END SUB
Calling a SUB procedure
Function: CALL statement is used to execute the SUB procedure.
It transfers the program control to another procedure that you want
to call.
Syntax:[CALL] <procedure name> [(argument list)]
Where,
Argument list->lists of variables or constants passed to the procedure.
Example: CALL area(a,b)
Calling Module
The calling module is the part of the QBASIC program that calls or invokes
another part of the program to perform a specific task.

Called Module
The called module is the part of the QBASIC program that gets called by the
calling module to perform a specific task.
Syntax of SUB procedure Program

Calling module

Called module
WAP TO DISPLAY SUM OF TWO NUMBERS USING SUB PROCEDURE
Declaring the SUB procedure DECLARE SUB sum(a,b) parameters
ClS Procedure name
Input "Enter any two numbers"; a, b Main Module
Calling the procedure CALL sum(a, b)
END arguments

SUB sum ( a, b)
parameters
s=a+b
Defining SUB procedure SUB Module
PRINT "SUM OF TWO NUMBERS=";s
END SUB
Points to remember
• EXIT SUB can be used to exit from sub procedure.
• The number of arguments and number of parameters should be same
and of same data type.
Analyse the given program and answer the following questions
REM To calculate area of the rectangle
DECLARE SUB area(l,b)
CLS
INPUT "Enter the length: ; l
INPUT "Enter the width: “; b
CALL area(l, b)
END

SUB area (l,b)


ar = l * b
PRINT "The area of the rectangle is: "; ar
END SUB
Analyse the program:
1. What is the function of the first statement?
2. What type of procedure is used?
3. List the procedure and parameters name.
4. Write the function of statement no.6
5. Label procedure type, procedure name, parameters, calling and called
module in the given program.
WAP TO DISPLAY SUM OF TWO GIVEN NUMBERS 2,10 USING SUB PROCEDURE
DECLARE SUB SUM(a, b)
ClS
CALL SUM(2,10)
END

SUB SUM(a, b)
S=a+b
PRINT "SUM OF TWO NUMBERS=";S
END SUB
WAP using SUB procedure:

1. To enter any three numbers and display its sum.


2. To enter any three numbers and display its average.
3. To display the greatest number from the given numbers 5,34,100.
4. To display the simple interest by taking necessary input in the main module.
5. To calculate and display area of a rectangle.
6. To calculate and display area of a circle.
7. To calculate and display volume of a sphere.
8. To calculate and display area of a cube.
Q) To create a sub procedure, we have to do three main things they
are _____ , _______ and ________________.

declaring a sub procedure


defining sub procedure
calling a sub procedure
Argument and Parameter
• Arguments are the variables or constant which are enclosed in the
parenthesis of procedure calling statement and its values are supplied
to the procedure.
• Arguments are also known as actual parameter or real parameter.
• Argument can be a constant, variable or an expression
• There are two different methods of passing arguments to the procedure.
Passing argument by value method
Passing argument by reference method
Argument as an expression
WAP to enter a number and display its cube.
DECLARE SUB SQ(s)
CLS
INPUT “Enter a number"; a
CALL SQ(a^2)
END

SUB SQ(s)
PRINT “Square value=“: s
END SUB
Parameter

• The variables enclosed in the parenthesis of the procedure which accept


constants or variables passed to them from the calling module are
known as parameters.
• Parameters are also known as Formal Parameter/ Nominal Parameter.
(x)
Passing argument by Reference method
• It is the default method of passing argument to the procedure.
• The addresses of the variables are passed to the procedure. ie.
Parameter use the same memory addresses of the arguments.
• Since both argument and parameter use the same memory address
any changes in the value of parameter in the procedure changes the
value of argument in the calling module.
Passing Argument by Value method
• The direct values of arguments are passed to the parameters
of the procedure.
• The values of arguments are copied to the parameters .So any
changes in the values of parameters do not affect the values
of arguments.
• Each argument is enclosed in individual parenthesis in the
calling statement.
(x)
Difference Between Passing arguments by reference Vs Passing arguments by value

Passing arguments by reference Passing arguments by value

When arguments are passed by When arguments are passed by value it only
reference the address of the variables are passes the values of variables to the
also passed to the procedure. procedure and doesn’t make any effect to
the original value of a variable.

It is the default mode of passing To pass argument by value method all


arguments arguments are enclosed in individual
parenthesis in the calling statements
Write down the output of the following program
DECLARE SUB TEST (A,B,C)
CLS
X=1:Y=2:Z=1
PRINT X,Y,Z
CALL TEST((X),(Y),(Z))
PRINT X,Y,Z
END

SUB TEST (A,B,C)


A=A+2
B=B+1
C=C+4
END SUB
SUB procedure can be called without passing argument
DECLARE SUB MUL()
CLS
CALL MUL
END

SUB MUL ()
FOR J=1 TO 10
PRINT 2*J
NEXT J
END SUB
WAP to enter any two numbers and display its sum without passing argument
DECLARE SUB SUM()
CLS
CALL SUM
END

SUB SUM ()
INPUT “Enter any two numbers”;a,b
S=a+b
PRINT “Sum of two numbers=“;S
END SUB
Write down the output of the following program
DECLARE SUB TEST (A,B,C)
CLS
X=5:Y=4:Z=10
PRINT X,Y,Z
CALL TEST(X,Y,Z)
PRINT X,Y,Z
END

SUB TEST (A,B,C)


A=A+2
B=B+1
C=C+4
END SUB
Write down the output of the following program
DECLARE SUB TEST (A,B,C)
CLS
X=1:Y=2:Z=15
PRINT X,Y,Z
CALL TEST(X,(Y),(Z))
PRINT X,Y,Z
END

SUB TEST (A,B,C)


A=A+1
B=B+2
C=C+3
END SUB
Re-Write the below programs after correcting the bugs:
a) REM to display greater among 2 numbers
DECLARE SUB great (p, q)
CLS
INPUT "Any two numbers "; a, b
DISPLAY great (p, q)
END

SUB great (a, b)


IF a < b THEN
PRINT a;
ELSE
PRINT b;
END IF
END SUB
Variable
A variable is a name given to a memory location that stores data.
This data can be changed during program execution.
Variables are essential in programming as they allow you to store
and manipulate data.

There are two categories of a variable.


Local Variable
Global Variable
Local Variable
• A variable which is declared inside any module and cannot be
accessed by other modules is known as local variable.
• It can be declared either implicitly or explicitly without using the
SHARED attribute of the DIM statement.
• The local variable defined in a procedure is needed to pass as an
argument to another procedure where it is to be used.
• The value of local variable gets destroy when the program flows
out of procedures.
Local Variable
WAP to display square of 5
DECLARE SUB SQ()
CLS
N=5
PRINT N^2
CALL SQ
END

SUB SQ()
PRINT N^2
END SUB
Global Variable
• Global variable is the variable that can be accessed from any
procedure or module used on the program. It is also known as
module level variable.
• It does not need to pass as an argument to use in any procedure.
• It is declared in the main module by using keyword COMMON
SHARED, DIM SHARED or SHARED.
Note: DIM SHARED and COMMON SHARED are only used in main
module but SHARED is used in SUB and FUNCTION procedure. It
shares the variable only with its main module.
Example: Global variable using DIM SHARED
DECLARE SUB AVERAGE ( )
DIM SHARED A,B,C
CLS
INPUT “Enter any three numbers”;a,b,c
CALL AVERAGE
END

SUB AVERAGE
AV=(a+b+c)/3
PRINT "Average of given numbers=";AV
END SUB
Example: global variable using COMMON SHARED
DECLARE SUB sum ( )
Common Shared A, B, C
Cls
Input "Enter any three numbers"; A, B, C
Call sum
End

Sub sum
s = (A + B + C)
Print "sum of given numbers="; s
End Sub
Example: global variable using SHARED
DECLARE SUB sum ( )
Cls
S=a+b+c
Print “sum=“;s
Call sum
End

Sub sum
Shared a,b,c
a=5
b=5
c=10
End Sub
Difference between local and global variables
Function Procedure

A Function procedure is a user defined function which is a small, logical and


manageable functional part of a program which performs the specific tasks and
returns value to the main module.
There are two types of user defined function:
i) Numeric Function
ii) String Function
Creating User defined Function
i) Function Declaration
ii) Function Definition
iii) Calling Function
DECLARE FUNCTION statement
Function: This statement is used to declare the user define function. It is non-
executable statement.

Syntax: DECLARE FUNCTION <function name>[(parameter list)]

Example: DECLARE FUNCTION area(a,b)


FUNCTION Definition
Function: Its function is to define the user defined Function Procedure
Syntax: FUNCTION <name> [(parameter list)]
Block of statements
<procedure name>=return value
END FUNCTION
Example: FUNCTION PROD(a,b)
p=a*b
PROD=P
END FUNCTION
Calling Function Procedure
Function Procedure can be called by using PRINT statement or a variable.

Syntax:
i) PRINT <Function name> []
Features of Function Procedure

• Function procedure are used in expressions and can directly return a


single value.
• It can be recursive.
• It has a data type on the basis of the return value of the function.
• Parameters can be passed by reference or by value method.
WAP to enter two numbers and display its sum using function procedure.

DECLARE FUNCTION SUM(a,b)


CLS
INPUT “Enter two numbers”;a,b
S=SUM(a,b)
Print “sum of two numbers=“;S
END
FUNCTION SUM(a,b)
S=a+b
SUM=S
END FUNCTION
WAP to enter a number and display it is odd or even.
DECLARE FUNCTION result$(n)
CLS
INPUT “Enter a number ”; n
Print result$(n)
END

FUNCTION result$(n)
IF n MOD2=0 THEN
result$=“Even Number”
ELSE
result$=“Odd Number”
END IF
END FUNCTION
Recursive function
DECLARE FUNCTION Factorial (n)
INPUT "Enter a number: ", num
IF num < 0 THEN
PRINT "Factorial is not defined for negative numbers."
ELSE
PRINT "The factorial of"; num; "is"; Factorial(num)
END IF

FUNCTION Factorial (n)


IF n = 0 THEN
Factorial = 1
ELSE
Factorial = n * Factorial(n - 1)
END IF
END FUNCTION
THANK YOU !

You might also like