Difference between revisions of "Blitz:General Syntax"

From Amiga Coding
Jump to: navigation, search
(Commands)
(Functions)
Line 24: Line 24:
  
 
==Functions==
 
==Functions==
Functions are similar to commands except they return a value to your program, which means you must do something with the value even if you don't need it. To differentiate from commands, arguments to functions need to be enclosed in brackets. For example:
+
Functions are similar to statements except they return a value to your program, which means you must do something with the value even if you don't need it. To differentiate from commands, arguments to functions need to be enclosed in brackets. For example:
 
<code>a = Abs(-25)</code>
 
<code>a = Abs(-25)</code>
 
The Abs function calculates and returns the absolute value of its argument, in this case 25. The value returned is stored in the variable a. The return value can also be used directly as a parameter in another function or statement, for example:
 
The Abs function calculates and returns the absolute value of its argument, in this case 25. The value returned is stored in the variable a. The return value can also be used directly as a parameter in another function or statement, for example:

Revision as of 17:55, 22 July 2015

Blitz Basic / AmiBlitz generally uses syntax common to most dialects of BASIC. Line numbers are not used; instead, each line in the source file is executed in turn starting from the top. Lines are entered as with any text editor, and moving to a different line either by using the arrow keys, clicking the mouse, or pressing Enter, tells Blitz to scan the line and detect any keywords for highlighting.


Variables

Variables are assigned a value using a single equals sign. For example: a = 10 b = a + 5 In this example, a contains the value 10 and b contains the value 15.

Keywords

In AmiBlitz 3, keywords by default will be highlighted as follows:

  • Built-in keywords are highlighted in blue
  • Built-in keywords that access the custom chips are highlighted in red (these will only work when running on classic Amiga hardware or an emulator)
  • Comments are highlighted in green italics
  • Constants are highlighted in magenta
  • Procedures (statements and functions) are highlighted in green
  • String constants are highlighted in grey


Statements

Statements are commands built into Blitz are keywords that are entered to carry out a specific action. They may accept parameters or arguments which are entered after the keyword and separated by a comma if more than one argument is required. Brackets are not used around the arguments. For example: Plot 20,30,2 The Plot command plots a single pixel on the currently used bitmap, and it takes 3 arguments. The first is the X coordinate, the second is the Y coordinate, and the third is the colour in which to draw the pixel. So, this example draws a single pixel 20 pixels across and 30 pixels down using pen 2 on the current bitmap.

Functions

Functions are similar to statements except they return a value to your program, which means you must do something with the value even if you don't need it. To differentiate from commands, arguments to functions need to be enclosed in brackets. For example: a = Abs(-25) The Abs function calculates and returns the absolute value of its argument, in this case 25. The value returned is stored in the variable a. The return value can also be used directly as a parameter in another function or statement, for example: NPrint "The absolute value of -25 is ", Abs(-25) This example uses the result of the Abs function as the second argument of the NPrint command. NPrint then displays the message followed by the result of the Abs function. Note that these functions are different to procedures that return values (also called functions).

Comments

Comments are extra text entered in your code that aren't part of the program itself but are used to make notes within your code. Blitz follows the same convention as AmigaDOS in using the semicolon character to mark the start of a comment. The comment ends at the end of the line. For example:

Calculate the Absolute value of variable a

b = Abs(a)

Now tell everyone

Nprint "Absolute value of ", a, " is ", b Comments can also be added after the working part of a line if desired: Plot x, y, 3 ; Drawing colour 3 at the coordinates x,y Comments are optional, but highly recommended! In a simple program they might not seem necessary, but once your code gets longer and more complicated, comments can help enormously when you try to understand what you were doing previously. They're even more important if there's ever a possibility that someone else will work on your code!