0% found this document useful (0 votes)
21 views33 pages

Online Class - X.PPTX First (Autosaved)

The document provides an overview of Modular Programming in QBASIC, including definitions, advantages, types of procedures (SUB and FUNCTION), and how to implement them. It explains the concepts of arguments and parameters, as well as local and global variables, with examples for better understanding. Additionally, it includes assignments related to computer networking and modular programming concepts.

Uploaded by

groshani269
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views33 pages

Online Class - X.PPTX First (Autosaved)

The document provides an overview of Modular Programming in QBASIC, including definitions, advantages, types of procedures (SUB and FUNCTION), and how to implement them. It explains the concepts of arguments and parameters, as well as local and global variables, with examples for better understanding. Additionally, it includes assignments related to computer networking and modular programming concepts.

Uploaded by

groshani269
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Class -X

Computer Science

MODULAR PROGRAMMING I
Modular Programming SEE – 8 Marks
On completion of this session students will be able to:
- Define Modular programming
- Say the advantages of Modular programming
- Say about the types of modules
- Types of procedure supported by QBASIC
- Differentiate between SUB and FUNCTION procedure
- Debug error full programs using SUB and FUNCTION procedure
- Discuss about the assignments
Main
Module

Sub Sub
Module 1 Module 2

Sub Sub Sub


Module 1.1 Module 1.2 Module 2.1
What is Modular programming?
• Modular programming is a programming technique used by
dividing a program into many small logical, manageable and
functional modules or blocks and later merging them.
• Every modular program has one main module and may have
many sub modules.
• The sub module of a program is also called procedure.
2. Write some advantages of modular programming

ANS : The advantages of modular programming are :


1. Easy for programmers : programmers can write the block or
modules of program independently
2. It reduces program codes : Same procedures can be used in
different numbers of places.
3. Increases the readability of program
4. Easy to debug the program
5. Easy to design program
3. Why QBASIC is called modular
programming language ?
Because :
• It allows the programmers to divide the program into
manageable and functional modules with the help of
sub procedure and function procedure.
4. What is Procedure in QB ? List its types .

• Procedure is a independent, manageable, logical and


functional part or block of codes to solve a particular
problem.
• It is also known as Sub Module or Sub program
• The types of procedures in QBASIC are :
• SUB Procedure
• FUNCTION procedure
5. Define SUB Procedure and write
its some important features.
A SUB procedure is a small manageable and functional part of a program that
performs specific tasks and does not return any value to the calling module.
A CALL statement is used to call the sub procedure module. A sub program is
written with SUB…END SUB statements.
• Features :
• it does not return any value
• sub procedure name doesn’t contain a type declaration symbol
• sub procedure name can’t be used as a variable
• sub procedure is called by CALL statement.
• arguments can be passed to the subprogram either by reference or by value
method
6. 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 may contain type declaration symbol
• A function name can be used as a variable in an expression
• Function procedure can be recursive
• arguments can be passed to the sub procedure by reference or by value
method
SUB Procedure FUNCTION Procedure

 SUB procedure does not return value  A FUNCTION procedure returns a


to the calling module. value to the calling module.

 SUB procedure name cant be used in  FUNCTION name can be used in an


an expression expression.

 FUNCTION procedure can be called


 SUB procedure is called using CALL by statement method using PRINT, IF
statement etc. or expression method .
( C$=CHECK$(N)
Assignments
a) Define LAN topology. Draw star topology. [SLC 2064, 2072, 2067, 2065 S , 2068 S, SLC 2069 S]
b) Give the importance of modem in the computer network. [SLC 2064]
c) What is computer network? [SLC 2066, 2068 S, 2069 S, 2070 S]
d) Write any two examples of data communication. [SLC 2067]
e) Write any two types of LAN topology.[SLC 2068]
f) Write any four advantages of computer network. [SLC 2069, SLC 2065 S]
g) What is network topology? Write any one advantage of star topology.[SLC 2070, 2065 S, 2066 S]
h) Give two-two difference between peer-to-peer and client/server network architecture. [SLC 2071]
i) What is bandwidth? [SLC 2066 S]
j) Draw the Bus topology. [SLC 2067 S, 2069 S]
k) Write any two reasons for connecting computer in a network. [2070 S]
l) What is communication channel? [SLC 2072]
Three major elements of modular
programming:
a) Declaration:
Specification of procedure name and its type, parameters and their types. We use
DECLARE statement for the specification the procedure.
Syntax: DECLARE [procedure type] <procedure name> ( parameter list)

Example: DECLARE SUB SimpleInt (P, T, R)


Example: DECLARE FUNCTION Check$ (X, Y, Z)
Example: DECLARE FUNCTION RevNum ( N)

We follow the same rules as in variables name declaration to declare a


procedure name
Three major elements of modular
programming:
b) Definition: The main part of the modular program where the logical statements
for solving a problem is written.
Syntax: [procedure type] <procedure name> (parameters)
<logical statement block>
<FUNCTION Name= value to be returned> only for FUNCTION procedure
END [procedure Type]
FUNCTION SimpleInt ( P, T, R )
Example: SUB SimpleInt ( P, T, R ) Si= P*T*R/100
SimpleInt=Si
Si= P*T*R/100
END FUNCTION
PRINT “ Simple Interest=“; Si
END SUB
Three major elements of modular
programming:
c) Calling (Execution): It is the real implementation of the program. Calling a
procedure refers to the execution of the procedure. We may pass arguments
to the procedure while executing the it.
Syntax: For SUB procedure
CALL <procedure name> (argument list)
Or <procedure name> argument list
Example: DECLARE SUB SimpleInt (P, T, R)
INPUT “Enter Principal, Time and Rate:” Pr, Ti, Ra
CALL SimpleInt (Pr, Ti, Ra)
END
Three major elements of modular
programming:
c) Calling (Execution):
Syntax: For FUNCTION procedure
we use either statement method or an expression method to call FUNCTION
procedure.
Syntax: Statement method
<Statement> FUNCTION Name (argument list)
Syntax: Expression method
<variable>= FUNCTION Name (argument list)
Three major elements of modular
programming:
DECLARE FUNCTION SimpleInt (P, T, R)
CLS
INPUT “Enter any Principal, Time and Rate”; Pr, Ti, Ra
PRINT “ Simple Interest=”; SimpleInt (Pr, Ti, Ra)
END

DECLARE FUNCTION SimpleInt (P, T, R)


CLS
INPUT “Enter any Principal, Time and Rate”; Pr, Ti, Ra
SI= SimpleInt (Pr, Ti, Ra)
PRINT “ Simple Interest=”; SI
END
Write a program to input a number and check whether the
number is odd or even.
DECLARE SUB OddEven (N)
CLS
INPUT “Enter a number:” A
CALL OddEven (A)
END
SUB OddEven (N)
IF N MOD 2=0 THEN
PRINT N; “is even”
ELSE
PRINT N; “is odd”
END IF
END SUB
Write a program to calculate the volume of a cylinder using FUNCTION……END FUNCTION.
[Hint: π*R^2*H]

DECLARE FUNCTION VOL(R,H)


CLS
INPUT “Enter radius and height”; Ra, He
PRINT “The volume=”; VOL(Ra,He)
END
FUNCTION VOL(R,H)
CONST PI=3.14
V=PI*R^2*H
VOL=V
END FUNCTION
To input a number and check whether the number is positive
negative or zero
DECLARE SUBTEST(N)
CLS
INPUT “Enter a number”; P
CALL TEST(P)
END
SUB TEST(N)
IF N>0 THEN
PRINT N; “is positive number”
ELSEIF N<0 THEN
PRINT N; “is negative number”
ELSE
PRINT The number is zero”
END IF
END SUB
DECLARE FUNCTION TEST$(N)
CLS
INPUT “Enter a number”; T
PRINT “ THEN NUMBER IS”, TEST$(T)
END
FUNCTION TEST$(N)
IF N>0 THEN
K$=“Positive”
ELSEIF N<0 THEN
K$=“Negative”
ELSE
K$=“Zero”
END IF
TEST$=K$
END FUNCTION
Write a program to display greater number among any two numbers using
FUNCTION procedure

DECLARE FUNCTION GREAT(A,B)


CLS
INPUT “Enter any two number”; A,B
PRINT “The greater number is”; GREAT(A,B)
END
FUNCTION GREAT(A,B)
IF A>B THEN
GREAT= A
ELSE
GREAT=B
END IF
END FUNCTION
Computer
Science
Modular
Programming II
On completion of this session, the
following objectives are expected to
be fulfilled:
• Review for identifying arguments and parameters in modular
programming
• Review of the methods of passing arguments
• Review of local and global variables with their implementation
• Question/answer session
• Provide assignments for the revision at home
What are arguments and
Parameters?
• Arguments: The constant and variable enclosed in
parenthesis of procedure call statement and that are
supplied to a procedure are known as arguments. They
are also known as actual/real parameters.

• 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 . They are also known as formal parameters.
Examples:
DECLARE SUB Avg( X, Y, Z)
DECLARE SUB Avg( X, Y, Z)
INPUT “Enter any three numbers”; A, B, C
CALL Avg (5, 10, 15)
CALL Avg (A, B, C)
END
END
SUB Avg (X, Y, Z)
SUB Avg (X, Y, Z)
Av= (A+B+C)/3
Av= (X+Y+Z)/3
PRINT “ Average of three numbers= “; Av
PRINT “ Average of three numbers= “; Av
END SUB
END SUB
In the above program:
In the above program:
The formal parameters are the variables that receives the
The formal parameters are the variables that receives the
value passed from the calling modules; they are specified
value passed from the calling modules; they are specified
in procedure definition; they are X, Y, Z
in procedure definition; they are X, Y, Z
The real parameters are the constants passed from the
The real parameters are the variables passed from the
calling modules and they are 5, 10 and 15.
calling modules and they are A, B and C
Passing Arguments
Arguments passing by reference
• Arguments are passed to a sub module by reference which gives
procedure access to the actual location of the variable . In this method ,
the address of each variable is passed to the procedure . The changes
made in the procedures variable will affect the value of arguments. It is
default mode of passing arguments .
for ex CALL avg(A, B,C)
Passing by value method :
• Arguments passed by value method does not make any effect to value of
the variable which are passed to procedure even they are changed in the
procedure. To pass argument by value method each arguments are
enclosed in individual parenthesis in the calling statements.
for ex CALL avg ((A),(B),(C))
Example: In the given program, Formal parameters are X, Y, Z

The values of the variables A, B and C are declared as 10, 20 and 30


DECLARE SUB Avg( X, Y, Z) respectively.
LET A= 10 When program control encounters CALL (A, B, C), it gets access to
LET B=20 the address of the variables A, B and C and pass the address of A, B
LET C= 30 and C to the Procedure
CALL Avg (A, B, C) Now the values of X, Y and Z will be the addresses of A, B and C
PRINT A, B, C respectively and calculates the value of Av by accessing the
END addresses of A, B and C and prints
Average of three numbers= 30
SUB Avg (X, Y, Z) After this, there is the change in the values of X, Y and Z and when
Av= (X+Y+Z)/3 END SUB is encountered then program control goes back to the
PRINT “ Average of three numbers= “; Av main module just below the CALL statement and print statement
X=X+3 will be encountered and displays the values of A, B and C
Y= Y+3
Z=Z+3 Can you guess what will be displayed as the value of A, B and C?
END SUB
It displays the value of A, B and C as 13, 23, 33
Example: In the given program, Formal parameters are X, Y, Z

DECLARE SUB Avg( X, Y, Z) The values of the variables A, B and C are declared as 10, 20 and 30
LET A= 10 respectively.
LET B=20
LET C= 30 When program control encounters CALL (A, B, C), it gets access to
CALL Avg ((A), (B), (C)) the address of the variables A, B and C and pass the address of A, B
PRINT A, B, C and C to the Procedure
END Now the values of X, Y and Z will be the addresses of A, B and C
respectively and calculates the value of Av by accessing the values
SUB Avg (X, Y, Z) of A, B and C and prints
Av= (X+Y+Z)/3 Average of three numbers= 30
PRINT “ Average of three numbers= “; Av
X=X+3 After this, there is the change in the values of X, Y and Z and when
Y= Y+3 END SUB is encountered then program control goes back to the
Z=Z+3 main module just below the CALL statement then print statement
END SUB will be encountered and displays the values of A, B and C

Can you guess what will be displayed as the value of A, B and C?


It displays the value of A, B and C as 10, 20, 30
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 variable . It is has its validity only in
the procedure where it is defined .

• A variable in main module or sub program which can be accessed


from other module or procedure of a program is known as global
variable . It has its validity in other part of the program.

• A global variable can be declared using the statement DIM SHARED/


COMMON SHARED or SHARED statement
• DIM SHARED statement is used to declare global variable in main module.
• Syntax:
• DIM SHARED variable
• Eg:
• DIM SHARED A

SHARED statement is used to declare global variable in sub module. It is used to make the variable
accessible which is declared at the module level without passing them as parameters.
• Syntax:
• SHARED variable
• Eg:
• SHARED A

The COMMON SHARED is a non executable statement that declares global variables for sharing
between modules (main program to SUB procedure or FUNCTION and vice versa).
• This statement declares the variable as global so that all modules can use this variable.
• Syntax: COMMON SHARED variable
• Example:
• COMMON SHARED A
Examples
DECLARE SUB TEST (P, Q) SUB TEST (A, B)
COMMON SHARED X S= A+B
A=5 PRINT S
B=10 X=X+10
CALL TEST (A,B) Y=30
PRINT Y; PRINT Y
PRINT X; PRINT X
END END SUB
Any Queries ?
Assignment
• a) Define LAN topology. Draw star topology. [SLC 2064, 2072, 2067, 2065 S , 2068 S, SLC 2069 S]
• b) Give the importance of modem in the computer network. [SLC 2064]
• c) What is computer network? [SLC 2066, 2068 S, 2069 S, 2070 S]
• d) Write any two examples of data communication. [SLC 2067]
• e) Write any two types of LAN topology.[SLC 2068]
• f) Write any four advantages of computer network. [SLC 2069, SLC 2065 S]
• g) What is network topology? Write any one advantage of star topology.[SLC 2070, 2065 S, 2066 S]
• h) Give two-two difference between peer-to-peer and client/server network architecture. [SLC 2071]
• i) What is bandwidth? [SLC 2066 S]
• j) Draw the Bus topology. [SLC 2067 S, 2069 S]
• k) Write any two reasons for connecting computer in a network. [2070 S]
• l) What is communication channel? [SLC 2072]

You might also like