- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement
Chapter 11: Writing Large Programs by Using Subprograms 155
Note that the EXIT SUB command simply tells Liberty BASIC to stop running the function and ignore any commands that come after the EXIT SUB command. In the above example, the EXIT SUB command has a 50 percent chance of running.
Using Functions
A function is a specialized subprogram that does nothing but calculate and return a single value. If you want to calculate the cube of a number (which is the same number multiplied by itself three times), for example, you can use the following instructions:
Cube = Number * Number * Number
But if you must calculate the cube of a number in several places throughout your program, you must type the formula Number * Number * Number in each place in your program. As you know, this practice is tedious and likely to create errors, especially if you want to modify the program later.
Defining a function
Rather than type similar instructions multiple times (using slightly different numbers), you can store instructions in a function. Then you pass data to the function, and the function spits back a single value. A function consists of the following four parts:
A name
One or more instructions for the function to calculate a single value
One line that assigns a value (or an expression that represents a value) to the function name
Any data that you want the function to use
A typical function in Liberty BASIC looks as follows:
FUNCTION FunctionName(Data)
‘ One or more instructions
FunctionName = value
END FUNCTION
Don’t leave a space between the function name and the left parenthesis, or
Liberty BASIC doesn’t run your program.
156 Part III: Advanced Programming with Liberty BASIC
Passing data to a function
Nearly all functions need to receive data from another part of your program. As another part of a program runs (or calls) a function, it needs to send (or pass) data to that function. To make sure that the function receives any data that another part of the program passes to it, you need to specify one or more variables in parentheses, as in the following example:
FUNCTION FunctionName(Variable)
‘ One or more instructions here
FunctionName = value
END FUNCTION
The preceding function can receive one chunk of data (such as a string or number) that another part of the program passes to it.
If your function needs to receive two or more chunks of data that another part of the program passes to it, just separate the variable names with commas, as in the following example:
FUNCTION FunctionName(Variable1, Variable2, Variable3)
‘ One or more instructions here
FunctionName = value
END FUNCTION
Don’t forget to define the type of data that the function accepts — for example, strings or numbers. (See Chapter 8 for more information about strings.) A complete declaration of variables may look as follows:
FUNCTION FunctionName(Note$, Salary)
‘ One or more instructions here
FunctionName = value
END FUNCTION
The first line of the preceding function defines the name of the function (which is FunctionName) and creates two variables, Note$ and Salary. The Note variable represents a string, and the Salary variable represents a single-precision value such as 3.14.
Calling a function
Of course, you want to use the function that you create in your program. A function represents a single value, such as an integer or a double-precision number, so you can treat the function name as a variable.
Suppose, for example, that you have a function that you call Cube, as in the following example:
Chapter 11: Writing Large Programs by Using Subprograms 157
FUNCTION Cube(Number)
Cube = Number * Number * Number
END FUNCTION
You can treat this function as you do any other variable, as the following examples show:
PRINT Cube(3)
or
MyValue = Cube(3)
The following breakdown shows how the computer interprets the first example, PRINT Cube (3):
1.The PRINT Cube(3) line tells the computer to run the function named Cube and “pass” it the number 3 as data. Print on-screen whatever value the Cube function calculates using the number 3.
2.Right away, the computer searches for the function Cube. The first line in the Cube function tells the computer that the function’s name is Cube and it needs an integer, which the variable Number represents — and in this case, it represents the number 3.
3.The second line in the Cube function tells the computer to multiply the value that the Number variable represents three times and to assign
this value to the function name of Cube. In this case, the value that the Number variable represents is 3, so the value of Cube is 3 * 3 * 3, or 27.
4.The third line in the Cube function tells the computer it’s the end of the function, so go back to the part of the program that originally called the Cube function. In this case, the computer goes back to the instruction
PRINT Cube(3).
5.Because the value of Cube(3) is actually 27, the computer reinterprets the instruction PRINT Cube(3) to actually mean PRINT 27. Thus the number 27 appears on-screen.
The complete Liberty BASIC program that calls the Cube function might look like this:
NOMAINWIN
PROMPT “Type a number:”; mynumber
NOTICE “This is the cube = “; Cube(mynumber)
END
FUNCTION Cube(Number)
Cube = Number * Number * Number
END FUNCTION
158 Part III: Advanced Programming with Liberty BASIC
Exiting prematurely from a function
A function acts like a miniature program that runs from start to finish. However, there may be some cases when you want to exit a function before it finishes running. To do this, you just need to insert the magic EXIT FUNCTION command such as:
NOMAINWIN
PROMPT “Type a number:”; mynumber
NOTICE “This is the cube = “; Cube(mynumber)
END
FUNCTION Cube(Number)
EXIT FUNCTION
Cube = Number * Number * Number
END FUNCTION
Note that the EXIT FUNCTION command simply tells Liberty BASIC to stop running the function and ignore any commands that come after the EXIT FUNCTION command. For more flexibility, you may want to use an IF-THEN statement to determine if a function should stop prematurely, such as:
FUNCTION Cube(Number)
IF (Condition = TRUE) THEN EXIT FUNCTION
Cube = Number * Number * Number
END FUNCTION
The above example simply checks to see if a certain condition is true; then it exits the function prematurely, which means that sometimes the entire function may run and sometimes it may exit prematurely.
Passing Data by Value or by Reference
When a subroutine or function needs data, your main program can pass it data in one of two ways:
By value
By reference
Obviously these two terms mean nothing to most people, so the plain-English translation means that if you pass data to a subroutine or function by value, your program actually creates two separate copies of the same data; one copy stays with the main program and a second copy gets used by the subroutine or function itself. Anything the subroutine or function does to the data remains isolated within that subroutine or function.
Chapter 11: Writing Large Programs by Using Subprograms 159
In the following code, the main program passes the data enemy$ to the MakeMessage subroutine function by value (which is the default method that Liberty BASIC passes data to a subroutine or function).
If you type the name Bobby Lee at the Prompt dialog box, the main program stores the string Bobby Lee into the enemy$ variable. When the main program passes the data by value to the MakeMessage subroutine, the MakeMessage subroutine creates a second copy of that same data, but stores it in a new variable called stuff$. At this time, both the enemy$ and stuff$ variable contain the string Bobby Lee.
The MakeMessage subroutine then modifies the value of the stuff$ variable. But because the main program originally passed the data by value, these changes never get returned back to the main program, so when the main program displays the value of the enemy$ variable, it’s just the original string of Bobby Lee as shown, in Figure 11-1.
NOMAINWIN
PROMPT “Give me the name of someone you hate:”; enemy$
CALL MakeMessage enemy$
NOTICE enemy$
END
SUB MakeMessage stuff$
stuff$ = “Bo the Cat hates “ + stuff$ + “ too!” END SUB
Figure 11-1:
When you pass data by value, a subroutine or function can alter data as much as it wants, but that altered data will never be returned back to the main program.
160 Part III: Advanced Programming with Liberty BASIC
If you pass data from the main program to the subroutine by reference, however, any changes made to the data within the subroutine or function will get passed back to the main program. To specify that you want a subroutine or function to get data passed to it by reference instead of by value, you have to use the magic BYREF command, such as:
SUB MakeMessage BYREF stuff$
Adding this single BYREF command to the DisplayMessage subroutine lets the subroutine pass any changes made to the data back to the main program, as shown in Figure 11-2.
NOMAINWIN
PROMPT “Give me the name of someone you hate:”; enemy$
CALL MakeMessage enemy$
NOTICE enemy$
END
SUB MakeMessage BYREF stuff$
stuff$ = “Bo the Cat hates “ + stuff$ + “ too!” END SUB
Figure 11-2:
When you pass data by reference using the
BYREF command, any changes that the subroutine or function makes to the data get passed back to the main program.
Chapter 12
Drawing Pictures and
Making Noise
In This Chapter
Making a graphics control
Toying with turtle graphics
Drawing circles and boxes
Adding sound
In the old days, computer programs printed data on a single strip of paper that consisted of one long scroll. So after computers eventually got monitors, most programs still displayed information on-screen the same as if they were printing on a scrolling piece of paper. Only after monitors were around for a while did programmers realize that they could write programs that use
fancy graphics, color, and sound.
So in this chapter, you can learn different ways to make your Liberty BASIC programs more colorful and visually appealing. After you add graphics and sound to your program, people will naturally find your program more interesting and (sometimes) easier to use.
Creating a Graphics Control
Before you can create and display graphics, you must create a graphics control, which is any window or part of a window that can display graphics on the screen. You can create a graphics control in the following two ways:
Create a separate graphics window
Create a graphics box inside an existing window