Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, February 7, 2022

SUB Procedure in QBASIC for class 10 and SEE

 

Understanding SUB Procedures in QBASIC: A Complete Guide for Class 10 and SEE

 

SUB Procedures in QBASIC


Introduction


In the world of programming, particularly for students preparing for their Class 10 and SEE exams, understanding the concept of procedures in QBASIC is critical. Procedures, also known as subroutines, are blocks of code that perform specific functions. They are essential for organizing code and improving readability. This guide will take you through SUB procedures in QBASIC, explaining their definition, usage, and key differences from FUNCTION procedures.

What are the SUB Procedures in QBASIC?


A SUB procedure in QBASIC is a structured block of code that performs a specific task. SUB procedures, unlike FUNCTION procedures, do not return any value. Instead, they enable you to divide your program into manageable sections, making it easier to maintain and comprehend. SUB procedures are defined with the SUB keyword and terminated with the END SUB statement.


Features of SUB procedure:

i)  It does not return any value

ii) Sub procedure name can't have a type of declaration characters

iii) Sub procedure name can't be used as a variable

iv) Sub procedure is called by CALL statement.

v) Arguments can be passed to the subprogram by reference and by value method


There are two types of procedures used in QBASIC. They are:

i) SUB procedure

ii) FUNCTION procedure


Difference between Sub procedure and Function procedure:

Sub procedure

Function procedure

Sub procedure does not return a value in main module

Function procedure returns a single value in main module.

It can be called by CALL statement.

It can be called by expression or PRINT statement.

The program code placed between SUB and END SUB statement.

The program code placed between FUNCTION and END FUNCTION statement.


SUB Procedure:

A sub procedure is a subroutine which is a block of statements to carry out a well defined task. A sub procedure does not return a value. Once a sub procedure defined, it can be called from the main module at any time. The block statements are written in sub procedure using SUB…..ENDSUB statement 


a) Declaring a Procedure

Before using any procedure in the QBASIC program, it must be declared first. The declaration of the procedure requires procedure name with the list of variable or constant to be passed to the procedure for processing. A procedure in QBASIC is declared using DECLARE statement. If you forget DECLARE the DECLARE statement to declare the procedure, QBASIC automatically inserts the DECLARE statement before the procedure name. The DECLARE statement consists of a procedure name along with a list of variables called arguments to be passed to the procedure. The DECLARE statement verifies the number of parameters and its types used in the procedure.

Syntax:

DECLARE SUB procedure_name [(parameter list)]

 

Where,

 

Procedure_name is the name of the SUB procedure. The name is valid QBASIC identifier an can be maximum of 40 characters.

Parameter list are the list of variables that are used in procedure.

 

Example:

DECLARE SUB sum(x,y)

Above example declares the sub-procedure sum with two parameters x and y. and y are numeric variables.


b) Defining a SUB procedure

After declaration of the procedure in the main module, it is necessary to define the procedure. SUB…….END SUB is a statement used to define sub procedure in the program. The procedure is define as same as the procedure is declared in the main program. The number of parameter list must match the number while defining the sub procedure.

When you type SUB keyword with name of the sub procedure, the QBASIC opens a new editing window with the procedure definition statement such as SUB……END SUB. 

After saving the program you can see the two names below the menu bar. i.e.

i)  Program name and             ii)  Procedure name

 


Note: To return the main module press F2 key and vice versa for sub module also.

After Pressing F2 key you can see the following dialog box:

 


Now you can select the desired module from the above dialog box and press the enter key to switch on the module.

Syntax:

SUB sub_name[(parameter_lists)]

               [statement blocks]

END SUB

Where,

Sub_name is the name of the sub procedure. The name can be written in the same way as variable name is written.

Parameter-lists are the list of variables which hold the referenced values passed from the main module in the form of arguments

Statement blocks contains valid QBASIC statements.

 Example:

  SUB sum(x,y)

              INPUT “Enter any two numbers”;x,y

              LET s= x+y

              PRINT “SUM=”;s

  END SUB

In above example sum is the procedure name. The sub procedure sum contains two parameters a and b.


c) Calling a sub procedure:

Once the procedure is defined in the program, it can be called anywhere in the program at any time. CALL statement is used to call a sub procedure. When the sub procedure is called using CALL statement, after the sub procedure the control of the program goes to CALL statement in the calling procedure.

 Syntax:

  CALL sub_name[(argument_lists)]

Where,

CALL is the keyword used to call the sub procedure

Sub_name is the name of the sub procedure to be called.

Argument_list are the list of variables or constant passed to the sub procedure for processing. The comma(,) is used to separated arguments.  

Example:

CALL sum(a,b)

In above example, “sum” is the sub procedure name that is to be called and “a” and “b” are arguments to be passed to the subprogram.

Example:  

WAP to input any two numbers in main module and display the average of given two numbers in sub module.

 

Note: In above program, the user supplies two different numbers, which are stored by the variables “a” and “b”. CALL statement is used to connect main module to sub module and ass the value of a and b of main module to sub module.  The average  of two numbers calculated in sub module and result is stored in variable "av" and display it in sub module only. 


Arguments and Parameters:

In a modular programming, there should be exchange of values between main module and sub modules. It is necessary to pass the data from main module to sub modules for processing. For the same, arguments and parameters are used.


Parameters:

Parameters are the variables which accept data passed to them. When we define a sub procedure, you specify a parameter list in parentheses immediately following the procedure name. We can indicate that a parameter is optional. It means that the calling code does not have to pass a value for it. 


Arguments:

The consonant and variable enclosed in parenthesis of procedure call statement and that are supplied to a procedure are known as arguments. Arguments are also known as actual parameters.  The arguments are used to pass the values to the procedure. Arguments are the constant values.  

Conclusion

SUB procedures in QBASIC are effective tools for structuring and managing your code. Understanding how to declare, define, and call SUB procedures enables you to break down complex problems into smaller, more manageable tasks. This improves code readability while also simplifying debugging and maintenance.

Call to Action.

Ready to improve your programming skills? Dive deeper into QBASIC to learn about FUNCTION procedures, file handling, and other programming concepts. Join our community for more tips and tutorials to help you ace your Class 10 and SEE exams!

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages