0% found this document useful (0 votes)
643 views

Notes On Modular Programming

Modular programming involves dividing a program into smaller logical modules or blocks. Each module performs a specific task. The main advantages are that it increases readability, makes debugging and design easier. QBASIC is a modular language as it allows dividing a program into sub procedures and function procedures. Sub procedures do not return values while function procedures return values. Modules can contain local or global variables. The document then provides examples of programs using sub and function procedures to perform tasks like calculating areas, checking number types, and more.

Uploaded by

Aayushi Pareek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
643 views

Notes On Modular Programming

Modular programming involves dividing a program into smaller logical modules or blocks. Each module performs a specific task. The main advantages are that it increases readability, makes debugging and design easier. QBASIC is a modular language as it allows dividing a program into sub procedures and function procedures. Sub procedures do not return values while function procedures return values. Modules can contain local or global variables. The document then provides examples of programs using sub and function procedures to perform tasks like calculating areas, checking number types, and more.

Uploaded by

Aayushi Pareek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

MODULAR

PROGRAMMING
MODULAR PROGRAMMING
• Modular programming is a technique used to divide our program into
many small logical, manageable and functional modules or blocks.
Every modular program has one main module and may have many
sub modules. The sub module of a program is also called as
procedure.

Advantages of modular programming


• Increases the readability of program
• Easy to debug the program
• Easy to design program
 Why QBASIC is called modular programming
language ?
• It allows the programmers to divide the program into
manageable and functional modules with the help of sub
procedure and function procedure

What is Procedure in QB ? List its types .


• Procedure is a independent manageable and functional
parts or blocks which solves a particular problem given by
user independently. It is also known as Sub Module or
Sub program
• Sub Procedure
• Function procedure
 Define Sub Procedure and write its some
important features.
• A sub procedure is a small manageable and functional
part of program that performs specific tasks and does not
return any value to the calling module.
• It does not return any value
• sub procedure name cant have a type declaration
characters
• sub procedure name cant be used as a variable
• sub procedure is called by CALL statement.
 Define function procedure and write its some
important features.
• A function procedure is a small manageable and
functional part of a program that performs specific tasks
and returns a value to the main program or calling
module. It is written with Function….END FUNCTION
statements.
• Features:
• Function procedure returns a value
• Function procedure name has type declaration characters
• A function name can be used in an expression
• Function procedure can be recursive
Differentiate between sub procedure and
function procedure.

• Sub procedure does not return value to the calling


module.
• A function procedure returns a value to the calling module.
• Sub procedure name cant be used in an expression
• Function name can be used in an expression.
• sub procedure is called using CALL statement
• Function procedure can be called by statement method
using print statement or expression method .
( C$=CHECK$(N)
•  
What are arguments and Parameters?
• The consonant and variable enclosed in parenthesis of
procedure call statement and that are supplied to a
procedure are known as arguments. It is also known as
actual parameters.
• Variables in a sub module or a procedure declaration
which accept data or variable passed to them from the
calling module are known as parameters . it is also known
as formal parameters
 Main Module and Sub Module
• The top level module is known as main module , which is
located at the top of all procedures.
• Sub module is a program which is written under the main
module . A  program may have one or more than one sub
modules
Local Variable and global Variable
• A variable which is defined in a module and is not
accessible to any other modules is known as local
variables . it is only accessible to the module where it is
defined .
• A variable in main module which can be accessed from
any module or procedure of a program is known as global
variable .
• Variable an be made global declaring them with DIM
SHARED , COMMON SHARED or SHARED attribute .
Examples to identify local and global variable

• DELARE SUB TEST (A,B)


• COMMON SHARED X
• A=5
• B=10
• CALL TEST (A,b)
• PRINT Y;
• END
• SUB TEST (P,Q)
• SHARED Y
• X=5
• Y=10
• PRINT P,Q
• END SUB
Write a program to calculate the volume of a cylinder using FUNCTION……

END FUNCTION . [Hint:  PI*R^2*H]


• DECLARE SUB VOLUME(R, H)
CLS
INPUT “ENTER RADIUS”; R
INPUT “ENTER HEIGHT”; H
CALL VOLUME(R, H)
END

SUB VOLUME (R, H)


V = 3.14 * R ^ 2 * H
PRINT “VOLUME OF CYLINDER ”; V
END SUB
Test whether the given number is positive or negative.
SUB PROCEDURE

DECLARE SUBTEST(N)
CLS
INPUT "Enter a number"; N
CALL TEST(N)
END
SUB TEST(N)
IF N>0 THEN
PRINT N; "is positive number"
ELSE
PRINT N; "is negative number"
END IF
END SUB
WAP to calculate perimeter of rectangle using sub
procedures

DECLARE SUB Peri (L, B)


CLS
INPUT "ENTER LENGTH"; L
INPUT "ENTER BREADTH"; B
CALL Peri(L, B)
END
SUB Peri(L, B)
P = 2 * (L + B)
PRINT "PERIMETER OF RECTANGLE "; P
END SUB
Accept any three different numbers and find the maximum
number among them.

DECLARE SUB MAX(A,B,C)


CLS
INPUT "Enter any three number"; A,B,C
CALL MAX(A,B,C)
END
SUB MAX(A,B,C)
IF A>B AND A>C THEN
PRINT A; "is maximum number"
ELSEIF B>A AND B>C THEN
PRINT B; "is maximum number"
ELSE
PRINT C; "is maximum number"
END IF
END SUB
WAP to convert Celsius into farenheit using sub
procedure
• DECLARE SUB CONVERT (C)
CLS
INPUT "ENTER TEMPERATURE IN CELCIUS"; C
CALL CONVERT(C)
END
SUB CONVERT (C)
F = C * (9 / 5) + 160
PRINT "TEMPERATURE IN FARENHEIT="; F
END SUB
 Convert Nepalese Currency to Indian Currency
using sub procedure
• DECLARE SUB CONV(N)
CLS
INPUT "ENTER NEPALESE CURRENCY "; P
CALL CONV(P)
END
SUB CONV (P)
C = P / 1.6
PRINT "INDIAN CURRENCY "; C
END SUB
WAP to check prime or composite using sub
procedure
• DECLARE SUB PRIME (N)
• CLS
• INPUT "ENTER ANY NUMBER"; N
• CALL PRIME (N)
• END

• SUB PRIME (N)


•C=0
• FOR I = 1 TO N
• IF N MOD I = 0 THEN C = C + 1
• NEXT I
• IF C = 2 THEN
• PRINT N; "IS PRIME NUMBER"
• ELSE
• PRINT N; "IS COMPOSITE NUMBER"
• END IF
• END SUB
Write a program to display the sum of individual digits of multi digit input number.

• Using Sub Procedure


• DECLARE SUB SUMDIG(N)
CLS
INPUT “ENTER MULTIGIT NUMBER” ; N
CALL SUMDIG(N)
END
• SUB SUMDIG(N)
WHILE N<>0
R=N MOD 10
S=S+R
N=N\10
WEND
PRINT “SUM IS ” ; S
END SUB
Write a program using sub procedure module to print the
series 1,1,2,3,5,8.. up to ten terms.
• DECLARE SUB SERIES ( )
• CLS
• CALL SERIES
• END
• SUB SERIES ( )
• A=1
•B=1
• FOR I = 1 TO 10
• PRINT A;
• C=A+B
• A=B
•B=C
• NEXT I
• END SUB
Write a program using Sub….End Sub to get a word from the user and
then print it in reverse order.          

•               
• DECLARE SUB REVERSE (N$)
• CLS
• INPUT “ENTER ANY STRING”; N$
• CALL REVERSE(N$)
• END
• SUB REVERSE (N$)
• FOR I = 1 TO LEN(N$)
• B$ = MID$(S$, I, 1) + B$
• NEXT I
• PRINT “REVERSED STRING IS “; B$
• END SUB
WAP to check wheather a number is armstrong or not using SUB
and SUB END…
• DECLARE SUB ARMS (N)
CLS
INPUT "ENTER ANY NUMBER"; N
CALL ARMS(N)
END
SUB ARMS (N)
A=N
S=0
WHILE N <> 0
R = N MOD 10
S=S+R^3
N = N \ 10
WEND
IF A = S THEN
PRINT A; "IS ARMSTRONG NUMBER"
ELSE
PRINT A; "IS NOT ARMSTRONG NUMBER"
END IF
END SUB
Write a program to generate the series using SUB …… END SUB : -10, -8,
-6, -4, ………. Up to 20th term.

• DECLARE SUB SERIES()


• CLS
• CALL SERIES
• END
• SUB SERIES
• A=-10
• FOR I = 1 TO 20
• PRINT A;
• A=A+2
• NEXT I
• END SUB
WAP to find area of Square using function END..
• DECLARE FUNCTION AREA (L)
• CLS
• INPUT “ENTER LENGTH”; L
• AR = AREA(L)
• PRINT “AREA OF SQUARE ”; AR
• END
• 
• FUNCTION AREA (L)
•A=L^2
• AREA = A
• END FUNCTION
write a program to check whether the input number is
positive, negative or zero.
• DECLARE FUNCTION CHECK$ (N)
• CLS
• INPUT “ENTER ANY NUMBER”; N
• PRINT N; “IS “; CHECK$(N)
• END
• 
• FUNCTION CHECK$ (N)
• IF N > 0 THEN
• CHECK$ = “POSITIVE NUMBER”
• ELSEIF N < 0 THEN
• CHECK$ = “NEGATIVE NUMBER”
• ELSE
• CHECK$ = “ZERO”
• END IF
• END FUNCTION
write a program that prints the sum of even digits
using function end procedure
• DECLARE FUNCTION SUMEVEN (N)
• CLS
• INPUT “ENTER ANY NUMBER”; N
• SU = SUMEVEN (N)
• PRINT “SUM OF EVEN DIGITS”; SU
• END
• 
• FUNCTION SUMEVEN (N)
•S=0
• WHILE N < > 0
• R = N MOD 10
• IF R MOD 2 = 0 THEN S = S + R
• N = N \ 10
• WEND
• SUMEVEN = S
• END FUNCTION
write a program to convert the temperature given in the centigrade to farenhiet. (9C +160/5)
 
• DECLARE FUNCTION CONVERT (C)
• CLS
• INPUT “ENTER TEMPERATURE IN CELSIUS”; C
• PRINT “TEMPERATURE IN FAHRENHEIT=”; CONVERT (C)
• END
• 
• FUNCTION CONVERT (C)
• F = C * (9 / 5) + 160
• CONVERT = F
• END FUNCTION
• 
write a program to find the perimeter of the rectangle.2(l+b)
 

• USING FUNCTION PROCEDURE


• 
• DECLARE FUNCTION PERIMETER (L, B)
• CLS
• INPUT “ENTER LENGTH”; L
• INPUT “ENTER BREADTH”; B
• PR = PERIMETER(L, B)
• PRINT “PERIMETER OF RECTANGLE ”; PR
• END
• 
• FUNCTION PERIMETER (L, B)
• P = 2 * (L + B)
• PERIMETER = P
• END FUNCTION

You might also like