September 26, 2020, 06:16:49 pm

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Basic Clarity 2 - Defines

Started by GWS, June 13, 2012, 06:13:00 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

June 13, 2012, 06:13:00 am Last Edit: June 13, 2012, 09:36:13 am by GWS
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:

Code Select

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



Tomorrow may be too late ..

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

GWS

Some people have eagle eyes ..  ;D ;D

Thanks Larry. :)

Graham
Tomorrow may be too late ..