07.05.2015 Views

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

Bronze Edition Guide - True BASIC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

108 BRONZE <strong>Edition</strong> <strong>Guide</strong><br />

Once you have defined a function in a DEF statement, you use that function simply by<br />

using its name where you would a variable. <strong>True</strong> <strong>BASIC</strong> carries out the instructions in the<br />

DEF statement and the resulting value is “returned” to the function name.<br />

———————————————————————————————————————<br />

x You must define a function before you use it in your program.<br />

———————————————————————————————————————<br />

If you don’t define it first, <strong>True</strong> <strong>BASIC</strong> won’t know that you are referring to a function and<br />

not a variable or array when you use the function name.<br />

Multi-line Functions<br />

You can also write multi-line functions to solve problems that require several lines of <strong>True</strong><br />

<strong>BASIC</strong> statements. DEF and END DEF statements define a multi-line function. As with<br />

one-line functions, you must define your multi-line functions before you use them.<br />

The SGN function is a multi-line function already built into <strong>True</strong> <strong>BASIC</strong>. SGN returns the<br />

sign of a number. That is, you give it a single number as an argument, and it returns:<br />

-1 if the number is negative<br />

0 if the number equals 0<br />

+1 if the number is positive<br />

You could easily define a SGN function yourself and test it as follows:<br />

! Define the Sgn function<br />

!<br />

DEF Sgn(x)<br />

SELECT CASE x<br />

CASE is < 0 ! If negative . . .<br />

LET Sgn = -1 ! . . .return -1<br />

CASE 0 ! If zero . . .<br />

LET Sgn = 0 ! . . .return a 0<br />

CASE else<br />

! Otherwise must be positive<br />

LET Sgn = +1 ! . . .return +1<br />

END SELECT<br />

END DEF<br />

INPUT n<br />

PRINT Sgn(n)<br />

PRINT Sgn(3-5*2)<br />

END<br />

! Input a number<br />

! Print its sign<br />

! And the sign of this formula

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!