Modular Programming QnA from main book (Page no.160) SEE

 

1)    What is modular programming?

Modular programming is a technique used to divide program into many small, manageable, logical and functional modules or blocks.

 

2)    What are the advantages of modular programming?

The advantages of modular programming are:

a)     Coding the program and testing is very easy.

b)    A module can be used in multiple places, which reduces the program codes.

c)     Different programmers can write different modules separately.

d)    Debugging of the program becomes easier and faster since they are divided into different modules.

 

3)    Differences between SUB-procedure and FUNCTION procedure

Sub Procedure

Function Procedure

1.     Sub procedure does not return a value.

1.     Function procedure returns a value.

2.     Sub procedure name does not accept data type identifier like %, $, !, &, #.

2.     Function procedure name accepts the data type identifier like %, $, !, &, #.

3.     Sub procedure is called using CALL statement.

3.     Function procedures are called using by statement method or using as expression. (using print statement).

4.     Its name can not be used in an expression.

4.     Its name can be used in an expression.

 

4)    What is the purpose and syntax of sub……end sub?

The SUB…..END SUB statement is a procedure statement that marks the beginning and ending of sub program. The syntax is:

SUB name [parameterlist]

[statements]

END SUB

 

5)    What is the purpose and syntax of CALL statement?

The call statement is used to transfer control to another procedure, a BASIC SUB program.

The syntax of CALL statement is:

CALL name (argument list)

 

 

 

 

6)    What is the function and syntax of Function….END FUNCTION statement?

The FUNCTION….END FUNCTION statement declares the name, the parameters, and the code that form the body of a FUNCTION procedure.

The syntax of FUNCTION…..END FUNCTION statement is:

FUNCTION name [parameter list]

[statements]

Function name=expression

[statements]

END FUNCTION

 

 

7)    Comparison Chart Between Global Variable and Local Variable

Global Variable

Local Variable

Global variables are declared outside all the function blocks.

Local Variables are declared within a function block.

The scope remains throughout the program.

The scope is limited and remains within the function only in which they are declared.

Any change in global variable affects the whole program, wherever it is being used.

Any change in the local variable does not affect other functions of the program.

A global variable exists in the program for the entire time the program is executed.

A local variable is created when the function is executed, and once the execution is finished, the variable is destroyed.

It can be accessed throughout the program by all the functions present in the program.

It can only be accessed by the function statements in which it is declared and not by the other functions.

If the global variable is not initialized, it takes zero by default.

If the local variable is not initialized, it takes the garbage value by default.

Global variables are stored in the data segment of memory.

Local variables are stored in a stack in memory.

We cannot declare many variables with the same name.

We can declare various variables with the same name but in other functions.

 

8)    What is the function and syntax of COMMON statement?

 

The COMMON statement is a non-executable statement that declares variables as global, so that they can be shared between main program, subprograms and functions. They appear only in the main module.

The syntax is: COMMON [SHARED] variablelist

 

 

 

 

9)    What is the function and syntax of DIM SHARED statement?

 

The DIM SHARED statement makes the variable accessible to all the modules. It appears in the main program.

The syntax is DIM SHARED variable (subscript)

 

 

 

 

 

 

 

1)    What is a modular programming?
Ans: Modular programming is a technique used to divide program into many small, manageable, logical and functional modules or blocks.

 

2)    What is module?
Ans: Module is a block of statement that solves a particular problem.

 

 

3)     What are the advantages of modular programming?
Ans: The advantages of modular programming are:
i) Different programmers can design different program modules independently, which is required in a large and complex program.
ii) It is easy to design code and test the program modules independently.
iii) It is possible to use a single module in different places which reduces program codes.

 

3)    What are two procedures QBASIC support to divide programs?
Ans: The two procedures used to divide programs in QBASIC are SUB-procedure and FUNCTION-procedure.

 

4)    What is SUB-procedure?
Ans: A SUB-procedure is a small, logical and manageable functional part of program which prefers specific task and does not return any value.

 

 

5)    What is a FUNCTION-procedure?
Ans: A FUNCTION-procedure is a small, logical and manageable functional part of a program which performs specific task and returns single value to the main program or calling module.

 

6)    Write down the function of CALL statement.
Ans: The function of CALL statement is to transfer the control to another procedure.

 

 

8)     Write down the function of DECLARE statement.
Ans: The function of DECLARE statement is to declare procedure such as FUNCTION or SUB in modular programming.

9)     What is main module?
Ans: The top level controlling section or the entry point in modular programming is called main module.

 

10)  What is sub module?
Ans: Sub module is a program which is written under the main module. A program may have one or more sub modules under main module.

 

11) Define parameters and arguments.
Ans: Parameters are variables that will receive data (arguments value) sent to the procedures (SUB program and FUNCTION).

Arguments are the values that are sent to the procedures (SUB program and FUNCTION)
* Actual or real parameters are called arguments.
* Formal parameters are called parameter

 

12) What is actual parameter?

Ans: The parameter passed to the procedure from the calling procedure statements are called actual parameters.

 

13) What is formal parameter?

Ans: The parameter in the procedure which receives the value from the actual parameters are called formal parameters.

 

14) Write down the functions of DIM SHARED statement.
Ans: The functions of DIM SHARED statement are:
i) It makes variable accessible to all modules.
ii) It appears in main module/ program.

 

15) What are library functions?
Ans: Library functions are built-in or readymade functions provided by QBASIC.

 

16) What is user defined function?
Ans: Function which is defined by the user according to the need is called user defined function.

 

17) Write down the differences between SUB and FUNCTION procedure.

SUB-procedure

FUNCTION-procedure

i) SUB-procedure does not return value.

i) FUNCTION-procedure must return a value.

ii) SUB-procedure is called by CALL statement.

ii) FUNCTION-procedure is called by statement and expression method.

iii) SUB-procedure’s name does not accept data type symbol because it does not need to return a value.

iii) FUNCTION-procedure’s name accepts data type symbols such as $, %, !, #, &, etc. and it depends on the type of value to be returned. E.g.: FUNCTION REV$ returns string.

 

18) Differentiate between SHARED and COMMON SHARED.
Ans:

SHARED

COMMON SHARED

It is used in the sub program to share the values of certain variables between main module and sub program

It is used in the main program to share variable list between main module and all sub programs.

 

19) Differentiate between local variable and global variable.
Ans:

Local Variable

Global Variable

i) Variables which are declared inside the procedure are called local variables.

i) Variables which are declared outside the procedure are called global variables.

ii) Local variables are not visible to other modules or functions.

ii) Global variables are visible to other modules or functions.

iii) Its value is protected from outside interference and has no effect on the variables outside the procedures.

iii) Its values can be accessed from any procedure or module.

 

 

20) Differentiate between passing argument by value and passing argument by reference
Ans:

Passing arguments by value

Passing arguments by reference

i) When arguments are passed by value it makes a duplicate copy of arguments and their values (constants) are used directly in parameter.

i) When arguments are passed by reference the address of the variables are passed to the procedure.

ii) It doesn’t make any effect on values of variable which are passed to a procedure even they are changed in the procedure.

ii) The changes made in the procedure’s variable will affect the variables used at calling module.

iii) To pass the argument by value, variable is enclosed in parenthesis.

iii) By default the value is passed by reference.

 

 

21) Why is large modules broken into small procedures?

Ans: Large modules are broken into small procedures to eliminate redundancy, for easier understanding, testing and debugging.

 

22) What are the major features of SUB procedure?

Ans: The major features of sub procedure are:

a.     It does not return any value.

b.     It does not have a data type.

c.     The parameter can be passed by reference or by value.

d.     They can be recursive.

 

 

 

23) What are the three important parts of SUB procedure? List with examples.

Ans: The three important parts of SUB procedure are:

       i.          Declaration of SUB procedure

Example: DECLARE SUB AREA (L, B)

     ii.          Body of SUB procedure

Example: SUB AREA (L, B)

                                        .

                                        .

                                        .

                                        END SUB

   iii.          Invocation of SUB procedure

Example: CALL AREA (L, B)

 

 

24) What is static variable?

Ans: The variable which is declared by using the “STATIC” keyword is called static variable.

 

25) What is automatic variable?

Ans: An automatic variable is a local variable which is allocated and de-allocated automatically when program flow enters and leaves the variable's scope. 

 

26) What is an array?

Ans: An array is a collection of multiple data elements stored under a common variable name.

 

27) What is recursion?

Ans: Recursion is a programming technique that allows the programmer to express operations in terms of themselves.

 









 

1)    WAP to input any number and display the factors.

 

DECLARE SUB FACT (N)

Input "ENTER ANY NUMBER"; N

Call FACT(N)

End

 

Sub FACT (N)

    Print "FACTORS OF"; N; "=";

    For I = 1 To N

        If N Mod I = 0 Then Print I;

    Next I

End Sub

 

 

 

2)    To check a number is palindrome or not

 

DECLARE SUB palindrome (N)

Cls

Input "Enter any number"; N

Call palindrome(N)

End

 

Sub palindrome (N)

    A = N

    S = 0

    While N <> 0

        R = N Mod 10

        S = S * 10 + R

        N = N \ 10

    Wend

    If A = S Then

        Print A; "is palindrome"

    Else

        Print A; "is not palindrome"

    End If

End Sub

 

 

 

 

 

 

 

3)    WAP to input number and count total no. of even digits.

 

DECLARE SUB COUNT (N)

CLS

INPUT "ENTER ANY NUMBER"; N

CALL COUNT (N)

END

 

SUB COUNT (N)

C = 0

WHILE N < > 0

R = N MOD 10

IF R MOD 2 = 0 THEN C = C + 1

N = N \ 10

WEND

PRINT "TOTAL NUMBER OF EVEN DIGITS"; C

END SUB

 

 

 

4)    To check given number is armstrong or not using sub procedure

 

DECLARE SUB ARM (N)

CLS

INPUT "ENTER ANY NUMBER"; N

CALL ARM(N)

END

 

SUB ARM (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

 

 

 

 

5)    To find the sum of individual digits of the given number

 

DECLARE SUB sum (n)

Cls

Input "Enter a number:"; n

Call sum(n)

End

 

Sub sum (n)

    While n <> 0

        r = n Mod 10

        s = s + r

        n = Int(n / 10)

    Wend

    Print s

End Sub

 

 

6)    Write a function procedure to read the side of a cube. Calculate its volume and surface area. (Hint: vol=side3 and sa=6 side2]

 

DECLARE FUNCTION TSAREA (L)

DECLARE FUNCTION VOLUME (L)

CLS

INPUT “ENTER LENGTH”; L

PRINT “TOTAL SURFACE AREA OF CUBE ”; TSAREA(L)

PRINT “VOLUME OF CUBE  ”; VOLUME(L)

END

 

FUNCTION TSAREA (L)

TSAREA = 6 * L ^ 2

END FUNCTION

 

FUNCTION VOLUME (L)

VOLUME = L ^ 3

END FUNCTION

 

 

 

 

 

 

 

7)    Write a program using FUNCTION…END FUNCTION to get radius of circle and then print its area and circumference.

 

DECLARE FUNCTION AREA (R)

DECLARE FUNCTION CIRCUM (R)

CLS

INPUT “ENTER RADIUS”; R

PRINT “AREA OF SQUARE ”; AREA(R)

PRINT “CIRCLE OF CIRCUMFERENCE”; CIRCUM (R)

END

 

FUNCTION AREA (R)

AREA = 3.14 * R ^ 2

END FUNCTION

 

FUNCTION CIRCUM (R)

CIRCUM = 2 * 3.14 * R

END FUNCTION

 

 

 

8)    WAP to enter any three numbers and display the greatest one.

 

DECLARE FUNCTION GREAT (A, B, C)

INPUT “ENTER ANY THREE NUMBERS”; A, B, C

PRINT “THE GREATEST NUMBER IS”; GREAT (A, B, C)

END

FUNCTION GREAT (A, B, C)

IF A > B AND A > C THEN

G = A

ELSEIF B > A AND B > C THEN

G = B

ELSE

G = C

END IF

GREAT = G

END FUNCTION

 

 

 

 

9)    Write a program using function end function sum(n) to print the sum of the first n natural numbers.

 

DECLARE FUNCTION SUM(N)

 

INPUT "ENTER THE NUMBER"; N

 

PRINT "SUM OF"; N; "NUMBERS="; SUM (N)

 

END

 

FUNCTION SUM (N)

 

S=N*(N+1)/2

 

SUM=S

 

END FUNCTION

 

 

 

 

10) WAP to input any number and check whether the given no. is Armstrong or not using function end function.

 

DECLARE FUNCTION ARM (N)

CLS

INPUT "ENTER ANY NUMBER"; N

A=N

AR = ARM (N)

IF A = AR THEN

PRINT A; "IS ARMSTRONG NUMBER"

ELSE

PRINT A; "IS NOT ARMSTRONG NUMBER"

END IF

END

 

FUNCTION ARM (N)

S = 0

WHILE N < > 0

R = N MOD 10

S = S + R ^ 3

N = N \ 10

WEND

ARM = S

END FUNCTION

 

Comments