FIX

Syntax: fix(a) as integer
Type: function

If the input is an integer fix will return the integer, otherwise fix will return the next integer closer to zero. For example fix(1.9) will return 1, and fix(-1.9) will return -1.

This is similar to int(), however int(-1.9) will return -2. With positive numbers there is no change.

See also int, cint.

flip

Category: gfx

Alias for PCOPY and SCREENCOPY. See SCREENCOPY for details.

Differences:
New to QB.

FOR...NEXT

Syntax: for var = value1 to value2 [step value3]
Type: statement
Category: control flow
also - exit for

Repeats execution of a code block a number of times, with a counter increasing/decreasing signifying which iteration the for loop is at.
FOR was accepting UDT fields as counter (v1c)

e.g. to print a triangle of stars:

height = 3
for i = 1 to height
print string$(i, "*")
next

Output:

*
**
***

Example of a counter: 
dim i as single
print "counting from 3 to 0, with a step of -0.2"
for i = 3 to 0 step -0.2
print "i is "; i
next

See also do...loop.

Differences:
Between QB:
doesn't support implicit pointers (ie: byref vars) as counters.

e.g.

sub foo( a as integer )
for a = 1 to 100
next a
end sub

'a' is an implicit pointer, and this is not supported.

FRE

Syntax: fre
Type: function
Category: Misc

Returns the free memory (ram) avaliable in bytes.


mem = fre
print "Free memory:"
print
print mem; " bytes"
print mem 1024; " kilobytes"
print mem (1024 * 1024); " megabytes"



See also dim, allocate.

Differences:
Between QB:
value doesn't matter, always returns the free physical mem available.

FREEFILE

Syntax: Freefile
Type: function

Returns the next open file slot.

e.g.


f = freefile
open "file.ext" for input as #f

FUNCTION

Syntax: [public | private] function name ([parameter[, parameter...]])
Type: statement
nameless arguments on function prototypes as in "BYVAL AS INTEGER" (v1c)

A small block of code used to perform a function based on input parameters, giving a return value. This can be used for example to see if the function performed properly or not using the return value. Functions are either public, or private (default is public).

Function is the same as sub except it allows a return value.
Variable-arguments, as in C: "sub foo(byval bar as integer, ...)" (v1c)
va_first(), va_arg() and va_next() intrinsic macros, to access variable-arguments in FB functions (v1c)

Example of functions:


function twice (x)
twice = x * 2
end function

print twice (10) 'will print 20, as it is twice of 10.



See also sub, exit, public, private.

GET (File I/O)

Syntax: Get #handle, [position], buffer
Type: statement

Reads data from a file in binary mode. Get will fill the variable you use as the buffer with data. If you use an integer as the buffer it will read 4 bytes (integer on 32-bit processor). Get will read any data type to the file in pure binary mode, making it a very versatile data reading command.

e.g.


dim buffer as byte
f = freefile
open "file.ext" for binary as #f
get #f, , buffer
close #f

See also put (file i/o), open.

GET (Graphics)

Syntax: GET [STEP] (x1, y1)-[STEP](x2, y2), arrayname[(idx)]
Type: statement
Category: Gfx

Statement to save a block of pixels data into an array for later use.
Gfx GET and PUT now support both arrays and generic pointers as data holders (lillo)

'(x1, y1)' and '(x2, y2)' are coordinates of the opposite corners of the graphics blocks to retrieve. The first is the upper-left corner, and the second is the lower-right corner.

'STEP' specifies a pair of coordinates is relative to the last graphics cursor position. STEP before the second pair of coordinates specifies those are relative to the first pair.

'arrayname' is the array where to save the graphical block.

'idx' is the index into 'arrayname' where to begin storing data. If idx is omitted, it is assumed to be 0.

Use this function to save blocks of graphics so you can later use [urlPUT (graphics)[/url] to draw them. The coordinates of the block are affected by the most recent WINDOW and VIEW (graphics) statements, and must be both inside the current clipping region set by VIEW (graphics). The supplied array must be large enough to hold the block you're going to save; the required array size depends on both the block size in pixels and the current color depth. Use the following formulas to compute the right size in bytes:

For color depths 1, 2, 4 and 8:

size = 4 + (w * h)


For color depths 15 and 16:

size = 4 + (w * h * 2)


For color depths 24 and 32:

size = 4 + (w * h * 4)



Where 'w' and 'h' are the width and height of the graphics block you want to save. Assuming you're working with INTEGER arrays, and as an INTEGER in FB is 4 bytes long, to save an 8bpp 32x32 block for example, you'll need an array of (((4 + (w * h)) + 3) / 4) elements.

For an example see put (graphics).

See also PUT (graphics), GET (File I/O), SCREEN (graphics), WINDOW, VIEW (graphics).

Differences:
Between QB:
Contrary to the QB version, this requested array size for an image does not reduce if you are using a mode with color depth less than 8bpp; so your arrays will need to be sized using the formula for 8bpp modes even if you're is 1, 2 or 4bpp modes. Also pay attention when storing pixel data blocks in multidimensional arrays, as QB stored arrays in column-order, whereas FB stores arrays in row-order, so what in QB was achieved with PUT (100, 100), sprites(0, 7), in FB is achieved with PUT (100, 100), sprites(7, 0).

getkey

Type: function

Differences:
New to FreeBasic.

getmouse

Syntax: GETMOUSE x, y[, [wheel][, [buttons]]]
Type: statement
Category: gfx

Statement to retrieve mouse position and buttons status.

Mouse position in screen coordinates is stored in 'x' and 'y' on function termination. If mouse is not present or out of the program window, 'x' and 'y' will hold -1.

'wheel' is the mouse wheel counter. Rotating the wheel away from you makes the count to increase, rotating towards you makes it to decrease. If mouse is not present or out of the program window, wheel will hold -1.

'buttons' stores the button status. On function termination, this will return a bitmask holding buttons status. Bit 0 is set if left mouse button is down; bit 1 is set if right mouse button is down; bit 2 is set if middle mouse button is down.

Use this function to get mouse info while in gfx mode. Pay attention: GETMOUSE does NOT work if a gfx mode is not set. Pass x, y, wheel and buttons variables by reference, so this function will store result values in them. If mouse is not present or out of the program window, all returned values will be -1.

e.g.


' Set video mode and enter loop
DIM x AS INTEGER, y AS INTEGER, buttons AS INTEGER
SCREEN 13
DO
' Get mouse x, y and buttons. Discard wheel position.
GETMOUSE x, y,, buttons
'buttons
LOCATE 1, 1
IF x < 0 THEN
PRINT "Mouse not available or not on window"
ELSE
PRINT USING "Mouse position: ###:### Buttons: "; x; y;
IF buttons AND 1 THEN PRINT "L";
IF buttons AND 2 THEN PRINT "R";
IF buttons AND 4 THEN PRINT "M";
PRINT " "
END IF
LOOP WHILE INKEY$ = ""
END



See also SCREEN (graphics), MULTIKEY, getkey.

Differences:
New to FreeBasic.

GOSUB

Syntax: gosub label
Type: statement

Jumps to a line label and remembers where it came from. Similar to using a sub except it is how it was done with the early basic interpreters.

Gosub is thought as bad programming practice as it can generate unreadable and untracable code. It is better to use sub or function instead.

e.g.


gosub message
end

message:
print "Welcome!
return



See also goto, sub, function.

GOTO

Syntax: goto label
Type: statement

Jumps code execution to a line label.

e.g.

1 goto 3
2 end
3 print "Welcome!
4 goto 2

You may be thinking that code is ugly and unreadable. In all truth it is, and goto's should be avoided for more modern structures such as do...loop, for...next, sub, and function.

See also gosub, sub, function.