Parenthesis

In BASIC

Parenthesis in BASIC can be used in a few different ways.

In an Array

Parenthesis are used in arrays to access its individual cells.

DIM someArray(10) AS STRING
someArray(1) = "First Cell"
someArray(2) = "Second Cell"
' etc..
PRINT someArray(1)
PRINT someArray(2)

NOTE: C-syntax programming languages use brackets for this purpose.

In Subroutines and Functions

Parenthesis are used to pass arguments to subroutines and functions. The argument itself is between the parenthesis.

SUB printSomething ( theArgument )
    PRINT theArgument
END SUB

printSometing ( "Zapp" )

Subroutines used as arguments are executed first and the result is used as the actual argument. This is sometimes called "nesting". For example :

RANDOMIZE TIMER
DIM randNumb AS INTEGER
randNumb = INT(RND(1)*256)+1

Looking at randNumb = INT(RND(1)*256)+1, this happens and in this order :

First, RND(1) generates a random number between 0 and less than 1.

randNumb = INT(0.32*256)+1

Since the result of RND(1) 0.32 is then multiplied by 256, it must be done before INT() is executed.

randNumb = INT(81.92)+1

Then INT() removes the decimal fraction (the part past the decimal point) from 81.92.

randNumb = 81+1

The last part isn't a part about nested functions, so you can skip it if you want. However, it is included for completeness.

Then, 81 and 1 are added.

randNumb = 82

And finally, 82 is put in the variable randNumb.

Precedence (Order of Operations)

Parenthesis are used to control the order in which mathematical operations are calculated. The operations in the "deepest" level of parenthesis are done first, followed by the next level and etc. This is known as precedence and order of operations. For example :

PRINT 4+((1+2)*3)

First, 1 and 2 are added together.

PRINT 4+(3*3)

Then 3 and 3 are multiplied.

PRINT 4+9

Finally, 4 is added to 9 and the result is printed.

This article is a stub. You can help improve Game Design Novice by expanding it.
page_revision: 1, last_edited: 1204134934|%e %b %Y, %H:%M %Z (%O ago)