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 declaration part where we declare sub procedure, another is main part from where sub part is called and last one is SUB part where we define our specific task to do. To understand this 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
//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js
(adsbygoogle = window.adsbygoogle || []).push({});
In the above program we declared the sub procedure by using DECLARE keyword and statements between CLS and END part is main part where code runs and control flow transfers to sub part by CALL keyword followed by sub name as a result the sub part gets executed and we see 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.
Lets see another example of sub procedure. We can pass value in sub procedure from the main part of the program by using perimeters.
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
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
Table of Content