Arrays from the dbpro help
help > principles > data types and variables
ARRAYS
Arrays are going to be a very important part of your future programs. They allow you to store large amounts of data under a single name. You can then access the data by index rather than by name alone.
If you had to write a program that stored each weeks lottery numbers, typing out 52 unique variable names is a lot of work, hard to maintain and quite unnecessary. Arrays allow you to create a special kind of variable that can store more than one item of data. You might start your program like this:
lottery1$="43,76,12,34,12,11"
lottery2$="76,12,34,12,11,44"
lottery3$="12,34,12,02,05,07"
etc..
Two hours later, you realize you could have written it like this:
DIM lottery$(52)
lottery$(1)="43,76,12,34,12,11"
lottery$(2)="76,12,34,12,11,44"
lottery$(3)="12,34,12,02,05,07"
etc..
We declare a string array using the DIM command followed by a name for our array. Like variables, when we use a dollar symbol after the name we instruct the program to use the array to store only strings. We then enclose in brackets how many items of data we wish the array to store. The array can be filled almost like a variable, but you must also provide the position within the array you wish to store your data.
But you then ask yourself what benefits I would have gained using the second approach. If you where also required to print out all 52 lottery numbers to the screen with your first approach you would have to add another 52 statements that printed each variable:
PRINT lottery1$
PRINT lottery2$
PRINT lottery3$
etc..
But if you had used an array, the same example would look like this:
PRINT lottery$(1)
PRINT lottery$(2)
PRINT lottery$(3)
etc..
You will have noticed that by using an array, you no longer have to refer to your data using a unique variable name. You can now point to the data you want using a position number. Accessing data this way has a thousand advantages over trying to access data by variable name alone, as you will discover. One example would be to improve the above like this:
FOR T=1 TO 52
PRINT lottery$(T)
NEXT T
Incredibly the above code replaced 52 PRINT statements with just 3 statements. With the above example, T is incremented from 1 to 52 within a loop that prints out the contents of the array at that position.
Arrays can also store multiple levels of data. At the moment our lottery entries are stored as strings and the numbers are hard to get at. Let's say we wanted to store all six numbers for every lottery week, we would create an array like this:
DIM lottery(52,6)
Without the dollar symbol($), we are declaring the array to store integer numbers instead of strings. You will also notice we have a second number separated by a comma. This means for every array position from 1 to 52, there is a sub-set numbered 1 to 6 in which multiple data can be stored. You can visualize an array as a filing cabinet with large draws numbered 1 to 52. Within each of the 52 draws is a tray with 6 boxes inside. You can store a value in each box. In all you can store 312 (52 x 6) values in this array. You can have up to five dimensions in your array, which means you can create an array as big as (1,2,3,4,5). Be careful when declaring dimensions, as large arrays consume large amounts of memory and may reduce overall performance of your program.
Entering data into our new array is elementary:
lottery(1,1)=43
lottery(1,2)=76
lottery(1,3)=12
lottery(1,4)=34
lottery(1,5)=12
lottery(1,6)=11
lottery(2,1)=43
lottery(2,2)=76
lottery(2,3)=12
lottery(2,4)=34
lottery(2,5)=12
lottery(2,6)=11
You are now able to give your program access to much more useful data. Unlike the string approach, you could make your program count how many times a certain number has appeared.
As you have determined, arrays need to be declared as a particular type. You can have an array of integer numbers, real numbers or strings. You cannot have multiple types in the same array, but you can declare new arrays dedicated to holding such data.
You can also declare arrays as global or local. Global arrays are the ones you are familiar with, and can be accessed by any part of the program. Local arrays can only be accessed by the function in which it was created. It is important to note that the global array must be delcared at the top of the main source code of the program as arrays are dynamically created only when the DIM command is executed. Placing DIM commands at the top of included source code will not dynamically create the array unless it lies within a subroutine called from the main program.
fumctions from the dbpro help
help > principles > functions
USER DEFINED FUNCTIONS
There will come a time when the ability to create your own functions will be priceless. Experienced programmers would not be able to write effective code without them. Although GOSUB commands and subroutines have been provided for compatibility and learning, it is expected that you will progress to use functions as soon as possible.
Functions are blocks of commands that usually perform a recursive or isolated task that is frequently used by your program. Variables and arrays used within the function are isolated from the rest of the program. If you use a variable name of FRED in your function, it will not affect another variable called FRED in your main program, nor any other function that happens to use a similar variable name. This may seem to be a restriction, but forces you to think about cutting up your program into exclusive tasks which is a very important lesson.
You can pass up to 255 parameters into your function, and have the option of returning a value when the function returns. Functions that do not return a value can also be used as normal commands in your main program.
Declaring a function couldn't be simpler. To use the FUNCTION command, simply provide it with a name and a list of parameters in brackets and your declaration is half-complete. Enter the commands you want on the following lines and then end the function declaration with the command ENDFUNCTION. The following example declares a function that returns half of a value passed in:
FUNCTION halfvalue(value)
value=value/2
ENDFUNCTION value
This declaration creates a function that can be used as a better print command:
REM Start of program
BetterPrint(10, 10, "Hello world")
END
FUNCTION BetterPrint(x, y, t$)
SET CURSOR x,y
PRINT t$
ENDFUNCTION
One thing the help doesn't mention about functions is variable scope. I can't say this is how it is in dbc but it in dbpro.
Arrays are global
normal variables can be made global with the global command;
global a#
global a as float
global variables can be read and written to within all code.
normal variables declaired out of funtions can not be set or read from within a funtion so if you want to have access to a variables data you need to pass it to the function or make it global or store in in an array.
I think variables declaired in functions are local by default. You can also use the local keyword;
local a#
local a as float
Local variables can be read/writen to only by their parent function. You can give variables outside of the function the same name and they are seperate from each other. Local variables do not keep their value between function calls.
I think that is correct, I am sure someone will correct me if anything is wrong
dbpro : 2ghz p4m : 512mb : geforce 4 4200 go