QB64.com

QB64 is a modern extended BASIC programming language that retains QBasic/QuickBASIC 4.5 compatibility and compiles native binaries for Windows, Linux, and macOS.

A FUNCTION block statement is used to create a function procedure to return a calculated value to a program.

Syntax

FUNCTION procedureName[type-suffix] [(parameters)]

{code}

‘variable definitions and procedure statements

procedureName = returnValue

END FUNCTION

Description

QBasic

Example(s)

A simple function that returns the current path. Place FUNCTION or SUB procedures after the program END.


PRINT "Current path = "; PATH$
END

FUNCTION PATH$
    f% = FREEFILE
    file$ = "D0Spath.inf" 'file name uses a zero to prevent an overwrite of existing file name
    SHELL _HIDE "CD > " + file$ 'send screen information to a created text file
    OPEN file$ FOR INPUT AS #f% 'file should exist with one line of text
    LINE INPUT #f%, PATH$ 'read file path text to function name
    CLOSE #f%
    KILL file$
END FUNCTION 

Returns a LONG array byte size required for a certain sized graphics screen pixel area GET (graphics statement).


INPUT "Enter a screen mode: ", mode%
INPUT "Enter image width: ", wide&
INPUT "Enter image depth: ", deep&
IntegerArray& = ImageBufferSize&(wide&, deep&, mode%) \ 2 ' returns size of an INTEGER array.
PRINT IntegerArray&
END

DEFINT A-Z
FUNCTION ImageBufferSize& (Wide&, Deep&, ScreenMode%)
    SELECT CASE ScreenMode%
        CASE 1: BPPlane = 2: Planes = 1
        CASE 2, 3, 4, 11: BPPlane = 1: Planes = 1
        CASE 7, 8, 9, 12: BPPlane = 1: Planes = 4
        CASE 10: BPPlane = 1: Planes = 2
        CASE 13: BPPlane = 8: Planes = 1
        CASE ELSE: BPPlane = 0
    END SELECT
    ImageBufferSize& = 4 + INT((Wide& * BPPlane + 7) / 8) * (Deep& * Planes) 'return the value to function name.
END FUNCTION 

Explanation: Function calculates the array byte size required when you GET (graphics statement) an area of a graphics SCREEN. Each mode may require a different sized array. Since graphics uses INTEGER arrays, 2 byte elements, the size returned is divided by 2 in the IntegerArray& calculation function reference. Function returns only 4 for SCREEN 0 which is a text only mode.

See Also