Free Web Site - Free Web Space and Site Hosting - Web Hosting - Internet Store and Ecommerce Solution Provider - High Speed Internet
Search the Web

Liberty BASIC Developer
LBDev ezine

 

Home
About
Current Issue
Past Issues
Files
Links
FAQ
Contact

Liberty BASIC Developer

Vol. 1 No. 5 November 2001

Contents:

Editors Note

LB News

Open Source: html editor

COMMAND Focus: Call/Sub

Site links

Next Month

Feed back

Legal Stuff

Editors Note

I hope that every one is doing well and getting their lives back to some level of normalance. I dont quite know were the month of October went but it came and went quickly which is why this issue is late. There are many promising programmers out there by the look at the message traffic that I have seen on the 3 yahoo news groups. I would like to give thanks to those who provide help to us all in the LB community and those who bring new ideas. Don't let that grey matter go soft.

Happy Programming

Neil Tremblay

top

LB News

Well it looks like we are getting closer to a beta release of LB3 if I am reading the messages correctly which have been posted. It also appears that Carl is working on a update of LB which will be 2.03.This will fix up some of the minor bugs.

If you haven't stopped by the Liberty BASIC site recently you may wish to take a look. There is information about a new SAMs book on programming using LB. Also Carl has an offering of online courses. As well there are links to other great Liberty BASIC resource sites.

top

Open Source: html editor Part 4

I dont know were the month went, but I did not get a chance to add any additional features to the HTML editor program. I have been able to create a small help file for it though. And of course the help file is in html format written with the editor. You can find the HELP file in the files section of this web site.

There is still plenty of room for refinement to this program. I would like to here from others who are using this program and what if any changes you may have made. I will not be doing any further changes for the time being, but I will probably come back to it a few month's from now.

Happy programming!

top

COMMAND Focus: Call/Sub

This month we are going to take a look at using the sub command. Sub is short for subroutine ( I know that this may be obvious but it still needs to be said). In Liberty Basic, subroutines are like seperate programs. They only know what you pass to them on calling. The only exceptions to this are handles and arrays which are global to the whole program. Here is the syntax for a subroutine.

sub mysub variables
 'some very important code goes here
end sub

Now lets go through each part of this command. The first thing is the reserved word 'sub' which tells the LB compiler that what follows is a subroutine. The next item 'mysub', which in this instance is the name which I have given to my subroutine. You may not name your subroutine with any reserved word! The last item in the first line is variables. You can have as many variables as you wish,numeric and/or string or none at all. These variables are how we pass information into the subroutine if they are needed to carry out a specific task that we assign. The second line is where we would place our code. This could be a simple single command or a complex multi line algorithm. The last line of our sample is 'end sub'. This signals to the compiler that it has reached the end of the subroutine and to return control to the next instruction after which it was called.

Lets start by creating a simple subroutine which plays a predefined wave file.

sub cheer
 playwave "tada.wav"
end sub

In this sample we have named our subroutine cheer, and have not assigned that it requires any variables. When this subroutine is called it will play the tada wave file, then return control back to the calling part of the program. Follows is the line of code to run this subroutine.

.
.
call cheer
.
.

This command has program flow temporarly redirected to the subroutine cheer. Which then performs its action of playing our selected wave file before returning. We will now make a call and subroutine which uses a single variable.

.
.
myNum = 8
call prtsq myNum
.
.

sub prtsq var 'Compute and print square of a number
 varsq = var * var
 print varsq
end sub

In the above example we start by assigning 8 to the numeric variable myNum. We then make a call to the subroutine 'prtsq', passing to it our assigned value of 8 in the myNum variable. Now in our subroutine prtsq, var has the assigned value of 8. It then proceeds to assign to our variable varsq the value of var * var, which in this case will be 64. The subroutine then prints to the main window the computed value of 64. As we have reached the end sub line, control is now returned to the first command after our call to the prtsq subroutine. You should be aware that if we had of had the formula var = var * var that the myNum would not be change. This would also be true if in our subroutine we had named the variable the same as in the program, myNum. These are different variables! Another note on the above example is that we could have sent the literal '8' to the subroutine.

In our next example we will pass a string to a subroutine.

.
.
myString$ = "Liberty BASIC Programmer"
call prtLittle myString$
.
.
sub prtLittle var$ 'converts string to lower then prints
var$ = lower$(var$)
print var$
end sub

This example is similar to the previous, except that we are using a string variable. Lets work through it. Our first line assigns the string 'Liberty BASIC Programmer' to our variable myString$. Next we make a call to our subroutine prtLittle sending along our loaded variable myString$. Our subroutine accepts this information and then proceeds to act upon the data as we have laid out. In this instance we have decided that we want to convert our string to all lower case, then it is printed to our main window. As before when the end sub statement is reached, control is returned to the command immediately following our call.

With this next example, we will pass multi variables of mixed types.

.
.
volts = 120
vmod$ = "units"
amps = 15
amod$ ="units"
call pwrcalc volts,vmod$,amps,amod$
.
.
sub pwrcalc emf,emulti$,current,cmod$ ' power calc.
 if emulti$="kilo" then emf = emf *1000
 if emulti$ ="milli" then emf = emf /1000
 if cmod$="kilo" then current = current*1000
 if cmod$="milli" then current = current/1000
 tpower = emf *current
 print tpower
end sub

Looking at this example you will see that we have assigned values to 2 numeric variables and 2 string variables. We then make are call to the pwrcalc subroutine, but note that our list of variables is seperated by commas. It should also be noted that variable types in the call must match those of the sub, being numeric or string. It is the programmers responsiblity to ensure that this is done.

There is one last example that we will look at. This is having a subroutine operate on some data and then return it to the calling program. I do not think that this is the correct use of a subroutine and that a function should be used in its place. Using this method with a function you can return multiple results. So lets get started, here is our code.

.
.
dim var(10)
for counter = 10 to 100 step 10
var(counter/10) = counter
next counter
call totarry
print var(0)
.
.
end
sub totarry
var(0) = 0 for inc = 1 to 10
var(0) = var(0) + var(inc)
next inc
end sub

To start with we are going to dimension an array which we will use to pass information to and from our subroutine. The next 3 lines of code are used to load data into the array var(). We could just as easily assigned individual values to any number of the elements in this array. The fourth line of our code makes the call to our subroutine totarry, notice that we did not pass any variables into it as in this case they are implied. Now we jump down to our subroutine totarry. Here our subroutine knows that some data will be loaded into our array. It then begins to process this data and places the sum result into the element var(0). Upon completion it returns control to the first command following the call which in this case is a simple print statement which displays the result in the main window.

Hopefully this explanation of using subroutines will be helpful and may even spark new ideas for its usage.

top

Site links

http://www.libertybasic.com Liberty BASIC home page

HTML editor Open source, code and Help file

http://lcsoftlb.50megs.com Liberty BASIC software from the Left Coast. Editors home site

http://lbdev.5u.com Liberty BASIC Developer home page

top

Next Month

  • Serial Port Primer
  • Command Focus: Open "COM...

top

Feed back

All readers feed back is welcome whether its to do with this issue, future acticles or comments in general. Send your message to mailto:neiltrem@hotmail.com

Any one who wishes to submit an article for publication, please send it to the editor. mailto:neiltrem@hotmail.com

top

Legal Stuff

The Liberty BASIC Developer ezine and lbdev.5u.com are © Neil Tremblay 2001 all rights reserved.

top

 
Copyright (c) 2001 Neil Tremblay
1770