|
By: Kevin Picone I N D E X:
Introduction If you've never programmed before, or even if you have, PlayBASIC (like all programming languages), follows certain conventions on how it expects your program code to set out. While most of these are fairly universal, that doesn't help if this is the first time you've ever programmed. So lets go over the basics. Firstly, PlayBASIC expects program code to be arranged from Top to Bottom. So our program is processed starting at line #1, then line #2, line #3 and so on down the program code. Each line of code, can have one or more commands on it. To have multiple commands on a line, requires that each command be separated by the colon character :. E.g
If you cut and paste this example into the IDE(editor) and execute it (Press F5). This example will display the following,
This example uses three basic commands, Print, Sync and WaitKey While it's very simple, it does demonstrate the top -> down flow of our program code. You can however change a programs flow by using decision (see: if and looping commands (see Loops) which will be necessary in order to keep your program running. This is because when PlayBASIC reaches the end of your programs code, it will end your program. Therefore most programs are broken down into three main parts. When a program starts, we'll generally need to initialize a number of things. Ranging from the display settings, variables, arrays or load common pieces of media such as images or sounds. Once these are set up, our program is ready to start running it's Main Loop To keep a program running, we often have one central loop. This loop will generally either perform all the necessary work there and then, or call a list of sub routines / functions that perform the various tasks required. Functions and sub routines are a way for us to break large sections of program code down into smaller reusable parts (if need be). This can be a real life saver as projects get larger, since it not only helps to keep the length of our programs down, but it makes our program easier to read. Generally the simpler the code is to follow, the easier it'll be to maintain. (Ie. Find bugs, add new features etc) So a program (in format terms) might look something like this. (this is not real code by the way, it's just an example of the logic)
If you're not familiar with some of these terms, then it's highly recommended you make your way though the other included tutorials. See Variables, Loops, ArrayBasics, Functions&Psub, Types Compiler Passes PlayBASIC by design is a two pass compiler. Pass #1 is a pre-scan of the your program codes layout. During this pass, PB is only looking to identify Label names, Functions and Psub declarations. This pre-passing is what enables PlayBASIC to detect if a Labels exists or what parameters user defined functions use. Pass #2 is the compilation pass. This is where you code is compiled ready for execution. Variable Declarations PlayBASIC is a loosely typed language by default. This means that variables can be used anywhere in your program, without them needing to be explicitly declared previously. This is often more convenient for beginners in the short term, but can actually lead to 'typo' styled errors during the development of larger programs. However, you can also make PlayBASIC enforce variable declaration if you want using Explicit operator. When explicit is enabled, variables must be declared prior to being used. What about Global/Local/Static Variable Declarations ? Global/Local and Static variables are an exception. These variable types must be declared before they are used. eg. Correctly pre declared Local variables
eg. Incorrect use of Local variable declaration
Not sure what variables are ? then make sure you read the Variables Tutorial. Array Declarations Arrays require previous declaration before you can start using them. This means that an array has to be dimensioned some place prior to be the array being used (ie. storing or retrieving information from it). Ideally you will normally dimension arrays at the start of your program (your programs declaration area) or at the top of your include files, which will make the array declarations visible to the following code. eg. Correct Usage
eg. Incorrect Usage (This will throw a compiler error)
While you can dimension arrays at any point throughout a program, the array will only be visible to code that is bellow this declaration. eg.
Not sure what arrays are or how to used them ? then make sure you read the ArrayBasics Tutorial. Funtions & Psub Declarations Function & Psub's can be declared just about anywhere throughout your program. So unlike arrays, you can call your user defined functions prior to their declaration (see example bellow). However there are a few strict conventions you should be aware of.
ie Correct usage
ie InCorrect usage
Not sure what functions and Psub are? or how to use them? Then make sure you read the Functions&Psub tutorial. Label Declarations Labels can be declared all through your program. However there are some things you'll need to keep in mind when using them.
Do I Need Line Numbers ? No, line numbering is obsolete in modern BASIC, therefore PlayBASIC doesn't support line numbers. How Can I Continue lines ? PlayBASIC uses the underscore symbol _ (followed by a hard return) as it's continuation. What this does is left allows you to break long lines of code up into something that will fit on screen. You can use the continuation character pretty much any place you like. For example.
|
Related Info: | ArrayBasics | Functions&Psub | Images | Loops | Memory | NamingConventions | Operators | Types | Variables : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |