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

Saturday, March 5, 2022

Global variable in QBASIC for class 10 and SEE

 

Global variable in QBASIC  for class 10 and SEE

 


Introduction

For both newcomers and seasoned programmers, QBASIC has been a fundamental language in the world of programming. Global variables are one of the fundamental ideas that every QBASIC programmer needs to understand. Global variables will be thoroughly discussed in this article, along with their definition, usage, and importance in QBASIC programming.


What are Global variables?

Global variables are the variables which are declared outside all procedures and main program. They are available to all the blocks and modules. Thus, global variable can be used in whole program.


Why use Global Variables?

Global variables are useful for storing data that must be accessed from multiple parts of a program. For example, if you're writing a program to manage a database, you might declare a global variable to store the database file's name. This variable could then be accessed by any program subroutine or function that needs to read or write data to the database.

However, it is critical to use global variables with caution. If you use too many global variables, your program may become difficult to understand and maintain. Furthermore, if you are not cautious, global variables can cause errors if they are not used correctly.


Declaring Global Variables


In QBASIC, declaring a global variable is straightforward. You simply define it outside of any function or subroutine, at the beginning of your program.

In QBASIC global variables are declared by SHARED or COMMON/DIM SHARED statements.

Example of Using a Global Variable in QBASIC

By using SHARED statement:

SHARED is a statement that allows accessing the variables in every procedure declared at the main module without passing them as parameters.

Syntax:

SHARED variable_list

Where,

Variable_list(s) is the variable to be accessed in the program.

Example:

DECLARE SUB sum ()

DECLARE SUB diff ()

CLS

INPUT "Enter any two numbers"; a, b

CALL sum

CALL diff

END

 

SUB diff

SHARED a, b

su = a - b

PRINT "difference="; su

END SUB

 

SUB sum

SHARED a, b

s = a + b

PRINT "Sum="; s

END SUB

Note: in above example, there is no parameter but h the variables “a”, and “b” are shared in every module by SHARED statement.


By using COMMON SHARED statement:

COMMON is a non-executable statement that declares global variables for sharing between modules.

Syntax:

COMMON SHARED variable_list

Example:

COMMON SHARED w$

DECLARE SUB rev ()

DECLARE SUB vowel ()

CLS

INPUT "Enter any word"; w$

CALL rev

CALL vowel

END

 

SUB rev

FOR x = LEN(w$) TO 1 STEP -1

m$ = m$ + MID$(w$, x, 1)

NEXT

PRINT m$

END SUB

 

SUB vowel

FOR x = 1 TO LEN(w$)

m$ = MID$(w$, x, 1)

SELECT CASE m$

CASE "a", "e", "i", "o", "u"

PRINT m$

END SELECT

NEXT

END SUB

Note: In above example, a COMMON statement establishes storage for variable in a special area that allows them to be shared between modulus. The variable “w$” of main module can shared in both sub modules “rev” and “vowel”


By using DIM SHARED statement:

This statement makes the variable accessible to all the modules. It appears in the main program.

Syntax:

DIM SHARED variable(subscript)

Example:

WAP to input any five numbers and display them with their sum.

DIM SHARED num(5)

DECLARE SUB add ()

CLS

FOR x = 1 TO 5

INPUT "enter number"; num(x)

NEXT

CALL add

END

 

SUB add

FOR x = 1 TO 5

PRINT num(x)

s = s + num(x)

NEXT

PRINT "sum="; s

END SUB


Conclusion:

Global variables in QBASIC are useful for storing data that must be accessed from multiple parts of a program. However, global variables must be used with caution because they can make your program difficult to understand and maintain.



FAQs (Frequently Asked Questions)



1. Are global variables unique to QBASIC?

Ans: No, global variables are a concept found in many programming languages other than QBASIC. In terms of data sharing and accessibility, they serve similar functions.



2. Can global variables be modified by multiple functions simultaneously?

Ans: Yes, multiple functions can access and modify global variables at the same time. However, this can lead to potential synchronization issues, which must be carefully managed.

3. What happens if I declare two global variables with the same name?

Ans: QBASIC will treat them as separate variables, each with its own scope and data.

4. Can global variables be used in QB64, a modern QBASIC variant?

Ans:Yes, global variables work in QB64, just like they do in traditional QBASIC.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages