IonicWind Software

Creative Basic => General Questions => Topic started by: GWS on June 13, 2012, 06:13:00 AM

Title: Basic Clarity 2 - Defines
Post by: GWS on June 13, 2012, 06:13:00 AM
All programs need to specify the types (integer, string, etc) of the variables used.

This is because you usually get bad results if you try to copy one type of data into a different type.

Although Basic will try to figure out what type you are using automatically, it's probably best to be quite specific.

If you say: x = 4.2 for instance, Basic will set up memory to hold the contents of variable 'x' as a decimal quantity.

However, to make sure all your variables are set up exactly as you require them, use the AutoDefine "off" statement.

Then, when you run the program you will be notified of any variables that have not yet been defined.

Here's a little program to illustrate defining variables prior to their use:


openconsole
cls

' Basic Clarity 2 ..
' defining variables.

autodefine "off"

def i,j,k,a[4] as int
def x,y as float
def s as string

x = 5.123
j = 2
y = x ^ j
s = "The value of y is "

print s,y
do:until inkey$<>""
closeconsole
end



This example shows how multiple variables of the same type can be defined in one line.  Pretty neat.

Notice the Array definition a[4], setting up space in memory for four quantities of the integer type .. a[0], a[1], a[2], and a[3].   (Arrays always start at element '0' although you don't have to use it).

(As an alternative to using 'Def i as int', you could use 'Def i:int' or even 'Dim i:int'.  Your choice).

Setting out the working values for a program can't get much neater than that - although I have to concede that the IWB method 'Int i,j,k,a[4]' is slightly better.

best wishes, :)

Graham



Title: Re: Basic Clarity 2 - Defines
Post by: LarryMc on June 13, 2012, 07:53:31 AM
Graham, liking your little discussions.
Sorry to have to mention an error in this one.

if you define an array as a[4] the 4 indicates that there are 4 (not 5) elements in the array.
they would be addressed with  a[0], a[1], a[2], a[3]

It is stated clearly in the help file.

I know you did that just to see if we were paying attention. ;D
Title: Re: Basic Clarity 2 - Defines
Post by: GWS on June 13, 2012, 09:37:36 AM
Some people have eagle eyes ..  ;D ;D

Thanks Larry. :)

Graham