A Beginner's Guide to Freebasic
Total Page:16
File Type:pdf, Size:1020Kb
A Beginner’s Guide to FreeBasic Richard D. Clark Ebben Feagan A Clark Productions / HMCsoft Book Copyright (c) Ebben Feagan and Richard Clark. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". The source code was compiled under version .17b of the FreeBasic compiler and tested under Windows 2000 Professional and Ubuntu Linux 6.06. Later compiler versions may require changes to the source code to compile successfully and results may differ under different operating systems. All source code is released under version 2 of the Gnu Public License (http://www.gnu.org/copyleft/gpl.html). The source code is provided AS IS, WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Microsoft Windows®, Visual Basic® and QuickBasic® are registered trademarks and are copyright © Microsoft Corporation. Ubuntu is a registered trademark of Canonical Limited. 2 To all the members of the FreeBasic community, especially the developers. 3 Acknowledgments Writing a book is difficult business, especially a book on programming. It is impossible to know how to do everything in a particular language, and everyone learns something from the programming community. I have learned a multitude of things from the FreeBasic community and I want to send my thanks to all of those who have taken the time to post answers and examples to questions. I would also like to say a big thanks to the developers of FreeBasic. Without your continual hard work in development, we wouldn’t have what I consider the premier Open Source BASIC language for today. The PDF version of this book was created Using Open Office 2.0.2, http://www.openoffice.org/. 4 About the Book FBeginner is a concise, hands-on beginner’s guide to FreeBasic and is aimed at the novice programmer. The book assumes no prior programming knowledge. The goal of the book is to create a solid foundation in the basics of programming in FreeBasic that the programmer can build and expand upon. FreeBasic is a cross-platform compiler, however this book concentrates on using FreeBasic under the 32-bit Microsoft Windows™ and GNU/Linux operating systems. However, other than the installation and setup portions of this book, most of the code should run under any of the supported platforms. All of the source code presented in this book is original code, and can be downloaded from the Clark Productions website located at http://rdc.ascii-world.com/ or http://freebasic.hmcsoft.org/. 5 Conventions Used in the Book The various styles used in the book along with their meaning, are listed below. The basic paragraph format is the font you see here with each paragraph indented. Information that is supplemental to the topic being discussed will be presented in the format below. These colored boxes have additional, supplemental or historical information associated with the current topic being discussed. Source code is listed in a fixed font with borders to delimit the code segment. You should always type in the programs. Most are small and can be entered into the editor in just a couple of minutes. The physical act of typing in the programs will help you learn the vocabulary and structure of FreeBasic. You do not type in the line numbers; they are only for referencing the code in the text. The file name for the program is located after the code segment. 1 Option Explicit 2 Cls 3 Print "Hello World From FreeBasic!" 4 Sleep 5 End Listing 1: helloworld.bas Following each listing is an analysis paragraph which explains how the program works. It is marked by the word Analysis: in bold with a fixed font. Even though many of the keywords you will see may have not be discussed in detail in the text, seeing these keywords in action will help you become familiar with the vocabulary of FreeBasic. Analysis: This will mark the beginning of an analysis section. The code will be discussed using the line numbers referenced in the listing. You do not type the numbers in the editor. The output of a program is listed in a fixed font and enclosed in a gray box as shown below. This output is given so you can compare the results to see if you have entered the program correctly. Hello World from FreeBasic! Output 1: program.bas A potential problem is shown with a Caution paragraph. It is formatted similarly to the Analysis paragraph. Caution This style will alert you to potential problems and offer some remedies or tips to avoid them. 6 Table of Contents 1 A Brief Introduction to FreeBASIC..............................................................14 DIFFERENCES FROM QUICKBASIC...............................................................................................14 KEY FEATURES OF FREEBASIC..................................................................................................15 2 Numeric Data Types..................................................................................18 SIGNED VERSUS UNSIGNED DATA TYPES........................................................................................18 THE FLOATING POINT DATA TYPE................................................................................................19 POINTER DATA TYPES..............................................................................................................19 NUMERIC VARIABLES...............................................................................................................19 THE DIM STATEMENT..............................................................................................................20 SHARED VARIABLES................................................................................................................20 STATIC VARIABLES..................................................................................................................21 COMMON VARIABLES...............................................................................................................22 EXTERN AND IMPORT VARIABLES..................................................................................................22 DATA TYPE SUFFIXES..............................................................................................................22 CHANGING THE DEFAULT DATA TYPE............................................................................................23 USING DIFFERENT NUMBER FORMATS............................................................................................24 Hexadecimal Numbers................................................................................................24 Binary Numbers..........................................................................................................24 Octal Numbers............................................................................................................24 Exponential Numbers..................................................................................................24 WHICH DATA TYPE TO USE?....................................................................................................25 OPTION EXPLICIT...................................................................................................................25 A NOTE ABOUT OPTION EXPLICIT ...............................................................................................26 A LOOK AHEAD....................................................................................................................26 EXCERCISES.........................................................................................................................27 3 Converting Numeric Data Types.................................................................28 IMPLICIT DATA CONVERSION.......................................................................................................28 EXPLICIT DATA CONVERSION......................................................................................................30 Numeric Data Conversion Functions...........................................................................30 Using Conversion Functions in Macros........................................................................33 Decimal Rounding Functions.......................................................................................36 A LOOK AHEAD....................................................................................................................37 EXERCISES...........................................................................................................................38 4 Introduction to the C Runtime Library........................................................39 THE #INCLUDE DIRECTIVE........................................................................................................39 7 THE DECLARATION STATEMENT....................................................................................................41 RUNTIME CONVERSION FUNCTIONS...............................................................................................42 THE ADDRESSOF OPERATOR @.................................................................................................43 TESTING THE RUNTIME CONVERSION FUNCTIONS................................................................................45