- •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 147
Writing Modular Programs
If you’re really ambitious, you can write a large program as one huge list of instructions. The larger your program is, however, the harder reading, writing, and understanding the program become. Writing a large program as one set of instructions is like trying to build a house out of sand. Most likely, one part of the structure (such as a wall) is weaker than the rest of the structure and will eventually cause the whole thing to collapse.
Rather than create one huge program, programmers create a bunch of smaller programs and paste them together (sort of like using bricks to build a house). That way, if one part of the program doesn’t work, you can unplug that portion, rewrite or replace it, and leave the rest of your program unaffected.
In the computer world, little programs that make up part of a larger program are known as subprograms. Subprograms are also known as modules, hence the term modular programming.
Every modular program contains at least one subprogram, known as the main program. The main program usually does nothing more than tell the computer which subprograms to use next to accomplish a specific task.
A subprogram typically solves a single task, such as multiplying two numbers or verifying that the user types a correct password. For really complicated programs, you may have several subprograms that themselves break down into several smaller subprograms.
Suppose, for example, that you want to write a program to break into another computer. The overall task is simply as follows:
Break into another computer.
Of course, you can’t tell the computer just to break into another computer because it doesn’t know how to do it. You must tell the computer, in specific detail, exactly how to do what you want it to do. This task means defining the overall goal of the program by using smaller tasks, as follows:
1.Find the phone number of the target computer.
2.Guess a password to access the system.
3.After gaining access, beep to notify the user.
Ideally, you can solve each task by using a separate subprogram that’s completely independent of any other part of the program. After you get each subprogram to work correctly, you can paste all the subprograms together to create a larger working program.
148 Part III: Advanced Programming with Liberty BASIC
Programmers use subprograms for the following two reasons:
To make writing large programs easier: By writing a bunch of small programs that perform a specific task, you can paste together a bunch of subprograms to make a much larger program.
To store repetitive instructions in a single location: Sometimes you need to run the same set of instructions over and over again. Rather than write these instructions each time that you need them, you can write them just once and store them in a subprogram that any other part of your program can run at any time.
The main advantage of breaking a large program into subprograms is so that you can easily modify a large program just by modifying one or more of its smaller subprograms. Because small programs are easier to understand and modify than large ones, subprograms can help make your program more reliable.
To create a subprogram in Liberty BASIC, you need to perform the following tasks:
Write any subprogram instructions after the END command in your main program.
Identify the start of your subprogram with a unique name, enclosing it in square brackets [like this].
Identify the end of your subprogram by using the RETURN command, which tells the computer to return back to the part of the program that was running before the subprogram ran.
You can choose any name for your subprogram, but a good idea is to choose a descriptive name. If your subprogram prints a warning message to the user, for example, you may want to name your subprogram [warning].
The RETURN command identifies the end of your subprogram and tells the computer to return back to the main program.
So a typical subprogram may look as follows in Liberty BASIC:
Main program instructions
Main program instructions
END
[subprogram] Subprogram instructions Subprogram instructions RETURN
At this point, the computer totally ignores your subprogram because the computer starts at the top, beginning with the first instruction, and follows
Chapter 11: Writing Large Programs by Using Subprograms 149
each succeeding instruction until it reaches the END command, which tells the computer to stop running the program before it can reach any of the instructions that your subprogram stores.
So if you want the computer to run your subprogram instructions, you must use the GOSUB command, such as in the following line:
GOSUB [Subprogram name]
This command does nothing more than tell the program to jump to the subprogram that the label in brackets identifies. If you want your subprogram to run, you must insert a GOSUB command somewhere in your main program as follows:
Main program instructions1
GOSUB [subprogram]
Main program instructions2
END
[subprogram] Subprogram instructions Subprogram instructions RETURN
In the above example, the Main program instructions1 run and then the GOSUB command starts the subprogram running. After the subprogram instructions run, the RETURN command returns the computer to the line immediately following the GOSUB command. This makes the computer run the Main program instructions2 until it stops at the END command.
To see a real program that you can type and try yourself, take a look at the following example:
NOMAINWIN
PROMPT “What is your password?”; password$
IF password$ = “open” THEN
NOTICE “You typed a valid password.”
ELSE
NOTICE “Invalid password.”
GOSUB [hackeralert]
END IF
END
[hackeralert]
PROMPT “Are you a hacker? (Y or N)?”; answer$ IF answer$ = “Y” THEN
NOTICE “The police are on their way to pick you up now.” ELSE
NOTICE “Then you must just be incompetent.” END IF
RETURN