MODULAR PROGRAMMING atish shrestha 1 What does modular





















- Slides: 21

MODULAR PROGRAMMING ©atish shrestha

1) What does modular programming mean? Modular Programming is an approach in which the program is divided into separate independent units called modules. Each module performs a specific task. As the modules can be designed, tested and debugged separately, the task of programmer becomes convenient. ©atish shrestha

2) What are advantages/features of modular programming? The followings are the advantages/features of modular programming: a) It is easy to design, code and test the program modules separately. b) Program maintenance becomes easier. c) We can use the same module for any number of times in the program statements. This makes the program small and compact. d) It improves the readability of a program. ©atish shrestha

3) Define procedure. List its type? A procedure is a separate block of statements used by the module-level code. It can be called by the module-level code any number of times. A QBASIC program consists of module-level code and a number of procedures. There are two types of procedure in QBASIC: a) SUB procedure b) FUNCTION procedure ©atish shrestha D C I a P E F s s E

4) Define module. A module is a source file that can be separately compiled. It is a set of program statements which, when acting together, complete a specific task, and is identified by a name. ©atish shrestha

5) How many types of modules are there? Explain them. There are two types of modules. They are as follows: a) Main module: The main module is the controlling section of a modular programming. It controls the action of the program by calling the procedures under it. b) Sub module: A sub module is a program which is written under the main module. A modular program may have one or more than one sub module. ©atish shrestha

6) What is a DECLARE statement? Write its syntax. It is a non-executable statement that declares FUNCTION or SUB PROCEDURE and invokes argument type checking. syntax: DECLARE FUNCTION/SUB name (parameter list) where, name is the name that will be used to call the procedure parameter list indicates the number and type of arguments that will be used to call the procedure. ©atish shrestha

7) Write the syntax of SUB procedure statement. Syntax: SUB name (parameter list) statements block END SUB where, name is one unique name which can contain upto 40 characters. parameter list are variable names that will receive data (arguments) sent to the sub program ©atish shrestha

8) What is function procedure? A function is a self-contained program segment that carries out the special and well defined task. Functions are almost like sub procedures except that functions returns a value while sub procedures do not return value. ©atish shrestha

9) What is function? List the main types of function and define them. A function is a self-contained program segment that carries out the special and well defined task. There are two types of functions. They are: i) Library or built-in functions: These functions are provided by QBASIC software. eg. SQR( ), LEFT$( ), MID$( ) etc. ii) User Defined Functions: Functions, which are defined by the user according to the need, are called user defined function. ©atish shrestha

10) Write the syntax of FUNCTION…. END FUNCTION statement? Syntax: FUNCTION name (parameter list) statements Name = expression END FUNCTION where, name indicates the name of the FUNCTION and expression is a value (to be returned in the name) ©atish shrestha

11) Write the advantages of FUNCTION. The followings are the advantages of FUNCTION: a) Each function is reusable. b) It can be written, tested and debugged independently. c) Large numbers of programmers can be involved. d) Programs can be developed in short period of time. ©atish shrestha

12) Differentiate SUB procedure and FUNCTION in QBASIC. SUB FUNCTION i) It cannot return any i) It can return one and value. only one value. ii) CALL can be used to ii) CALL can’t be used to invoke SUBs. invoke FUNCTION. iii) SUB procedure do not iii) FUNCTION procedure have a data type. has a data type which is the return value of the FUNCTION. ©atish shrestha

13) What is a function prototype? A function prototype is a declaration of the function that tells the program about the type of value returned by the function and the number and type of arguments. ©atish shrestha

14) What do you mean by parameter? Parameters are variables that will receive data (argument values) to be sent to the procedures. ©atish shrestha

15) What are actual and formal parameters of a procedure? Formal Parameters are variables which are used to specify or declare types of data to be passed to the procedures. The parameters included in function declarations are called formal parameters (also called parameters). Actual Parameters are the variables or constants which are used to pass real value or data to the procedure. The parameter listed in a function call are called actual parameters (also called arguments). ©atish shrestha

16) In how many ways can we pass the arguments in function and procedure? We can use the following ways to pass the arguments in function and procedures: i) By reference ii) By value iii) Passing entire array into the function and procedure ©atish shrestha

17) Explain the followings: i) Passing arguments by reference: Passing arguments by reference gives the procedure access to the actual variable. The calling procedure passes the address of the variable in memory so that the procedure can change its value permanently. Passing by reference is the default mode of passing argument in QBASIC. ii) Passing arguments by value: when arguments are passed by value, the procedure sees only a copy of the argument. Passing arguments by value is useful when the function does not modify the values of the original variables in the calling program. This is done by enclosing the argument variable in parenthesis. ©atish shrestha

18) Give the differences between local and global variables. Local Variable Global Variable A local variable is declared inside a procedure, and is not accessible outside the procedure. Its life expires within the procedures itself. A global declared procedures accessed procedure. ©atish shrestha variable is outside all and can be from any

19) Give the differences between SHARED and COMMON SHARED It gives procedure access to It defines global variable that module level variable. can be shared throughout a program or between chained programs. ©atish shrestha

SHARED eg. COMMON SHARED eg. DECLARE SUB display ( ) DECLARE SUB add (a, b ) CLS COMMON SHARED s x=10 INPUT “enter two nos”; a, b CALL display CALL add (a, b) END PRINT s SUB display END SHARED x SUB add (a, b) ? “the value of x is”; x s=a+b END SUB Here, the variable which was declared in main program is used in sub program. ©atish shrestha