DarkBASIC Pro Tutorial: Functions

James Cloud
The definition of a function is simply a command or statement that give a value back after being executed. There are a number of different types of

functions built into DarkBASIC Pro such as math functions, input functions and many more. You can even make your own functions in DarkBASIC Pro.

An example of a math function in DarkBASIC Pro is the "ABS" function. This function simple converts a negative number or integer into a positive.

Example:

MyNumber = ABS(-25)

MyNumber will return a positive 25.

You can, of course, use regular mathematical expressions in DarkBASIC Professional, such as adding, subtracting, division, multiplication, and more

advanced mathematical expressions.

Example:

Math = A + B / C * A - 20

You can use functions in the same way and in the same places as you would use a string or a variable as long as the function returns the corresponding

value.

The "GET DIR$" function will return the string value of the current working directory, or the directory where your game is located.

You can retrieve the Windows directory or the My Documents Directory by using the "WINDIR$" and "MYDOCDIR$" functions.

There many other built in functions that come with DarkBASIC Professional to learn what these are read your DarkBASIC Manual or help file found under Help menu in the DarkBASIC Pro IDE.

User Created Functions in DarkBASIC Professional

All professional coders use functions to increase the reliability and usability of their code. Therefore it is vital that you learn how to incorporate your own custom functions into your DarkBASIC Pro game or application.

You should create a function when you want to use a large set of code over in over again throughout your application.

For example, if I wanted to execute 10 lines of code throughout my game instead of rewriting these 10 lines over and over again I could just put them into a function and then just execute that functions name when ever I wanted to use the code. This would cut 10 lines of code down to 1 simple line.

To make a custom function simply enclose your desired code in the "FUNCTION" and "ENDFUNCTION" tags. You will also need to give a name to your function.

Example:

FUNCTION DivideNumber(Number, BY)

Number = Number / BY

ENDFUNCTION Number

The above code will divide the number supplied in the first parameter by the number given in the second and return the results.

Published by James Cloud

I like to program and do basically anything that has to do with technology and computers.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.