It is a method to divide a large program into small programs. It doesn't return any value. CALL statement is used to invoke SUB Procedure in the main program. It has three parts, First one is the declaration part where we declare sub procedure, another is main part from where subpart is called and last one is SUB part where we define our specific task to do. To understand these 3 parts look at the following example.
DECLARE SUB sum
CLS
CALL sum
END
SUB sum
INPUT "Enter first number:"; x
INPUT "ENter second number: "; y
PRINT "The sum = "; x + y
END SUB
In the above program we declared the subprocedure by using DECLARE keyword and statements between CLS and END part is the main part where code runs and control flow transfers to subpart by CALL keyword followed by sub name, as a result, the subpart gets executed and we see the output. (Enter first number, ....). Again when SUB part ends then the control flow goes to main part where we find END statement. Finally, program terminated. This is how we use it generally.
Let's see another example of the subprocedure. We can pass value in subprocedure from the main part o
DECLARE SUB area(l,b)
CLS
INPUT "Enter length "; l
INPUT "Enter breadth "; b
CALL area(l, b)
END
SUB area (l, b)
a = l * b
PRINT "Area = "; a
END SUB
We can declare many sub procedures in one program as our need and we can call it in the main program many times. Lets observe the following example. (Better to run these programs in QBASIC IDE).
DECLARE SUB area(l,b)
DECLARE SUB volume(l,b,h)
DECLARE SUB perimeter(l,b)
CLS
INPUT "Enter length "; l
INPUT "Enter breadth "; b
INPUT "Enter height "; h
CALL area(l, b)
CALL perimeter(l, b)
CALL volume(l, b, h)
CALL area(l, b)
END
SUB area (l, b)
a = l * b
PRINT "Area = "; a
END SUB
SUB volume (l, b, h)
v = l * b * h
PRINT "Volume = "; v
END SUB
SUB perimeter (l, b)
p = 2 * (l + b)
PRINT "Perimeter = "; p
END SUB
Program to print whether the given number is odd or even.
DECLARE SUB OddEven(x)
CLS
INPUT "Enter any number "; x
CALL OddEven(x)
END
SUB OddEven (x)
IF x MOD 2 = 0 THEN
PRINT x; " is Even Number"
ELSE
PRINT x; " is Odd Number"
END IF
END SUB
We can use it with SELECT CASE statement to build better program. Look at the following example.
DECLARE SUB area(l,b)
DECLARE SUB volume(l,b,h)
DECLARE SUB perimeter(l,b)
top:
CLS
PRINT "**** MENU ****"
PRINT "1. Calculate Area "
PRINT "2. Calculate Volume "
PRINT "3. Calculate Perimeter"
INPUT "Enter your choice (1/2/3) :"; ch
SELECT CASE ch
CASE 1:
CALL area(l, b)
CASE 2:
CALL volume(l, b, h)
CASE 3:
CALL perimeter(l, b)
CASE ELSE
PRINT "Invalid choice !!.. Try again ...."
INPUT " "; anykey
GOTO top:
END SELECT
END
SUB area (l, b)
INPUT "Enter length : "; l
INPUT "Enter breadth : "; b
a = l * b
PRINT "Area = "; a
END SUB
SUB volume (l, b, h)
INPUT "Enter length : "; l
INPUT "Enter breadth : "; b
INPUT "Enter height : "; h
v = l * b * h
PRINT "Volume = "; v
END SUB
SUB perimeter (l, b)
INPUT "Enter length : "; l
INPUT "Enter breadth : "; b
p = 2 * (l + b)
PRINT "Perimeter = "; p
END SUB