Computer Science Education | Programming Language Tutorials For Beginners

QBASIC TUTORIAL 4 - Variables

VARIABLES
This section discusses an important topic in programming, "variables." Please read this section thoroughly.
A variable is a piece of data kept in the computer's memory (RAM). The location of a variable in RAM is called the "address."

The following program prints the variable X to the screen:

     print X

Since the variable hasn't been assigned a number, the value of the variable is 0. So, the output of
the program is:

    0

This next program sets X to 15, and then prints the variable:

   X = 15
   print X

This time, the output is:

   15

In the above example, the number 15 was stored in the computer's RAM at a certain memory
address. Then the PRINT command accessed (or looked at) that address when it printed "15" to
the screen.

(NOTE: The memory address of X is not necessarily 1000000)

ADVANCED TIP: Although you don't normally need to, you can find the actual memory address of a variable (X, for example) by using the VARSEG and VARPTR commands.

   PRINT (VARSEG(X) * 65536) + VARPTR(X)

As in the programs above, a variable is accessed by calling its name. Variable names can have a
combination of letters and numbers. The following are valid variables:

   Y
   num
   VALUE
   xYz
   abc123

Also, you can use multiple variables in your program.

   X = 82
   Y = 101
   Z = 79
   PRINT X
   PRINT Y
   PRINT Z


Output:

   82
   101
   79



(NOTE: The memory addresses of these variables are not necessarily as specified)

Share:

No comments:

Post a Comment

Popular Posts

Elitcode - Learning Start Here

Elitcode Blog Archive