July 23, 2024, 09:06:14 AM

## News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!

## Basic Clarity 2 - Defines

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

0 Members and 1 Guest are viewing this topic.

#### GWS

##### June 13, 2012, 06:13:00 AMLast 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:

`openconsolecls' Basic Clarity 2 ..' defining variables.autodefine "off"def i,j,k,a[4] as int def x,y as floatdef s as stringx = 5.123j = 2y = x ^ js = "The value of y is "print s,ydo:until inkey\$<>""closeconsoleend`

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

##### June 13, 2012, 07:53:31 AM #1
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.
LarryMc
Larry McCaughn
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

#### GWS

##### June 13, 2012, 09:37:36 AM #2
Some people have eagle eyes ..

Thanks Larry.

Graham
Tomorrow may be too late ..