Blitz2D Newbies: Functions and Returns
by CloudNine

FUNCTIONS

INTRODUCTION TO FUNCTIONS

Functions are basically a part of code neatly organised within your program, which can be used again and again. It makes your code neat and accessible, because all the functions you name will appear in the right hand column in the Blitz IDE.

RULES ABOUT FUNCTIONS

1) Functions must be placed outside the main code i.e. the While..Wend loop

2) They must always have End Function at the end of the function

FUNCTIONS WITHOUT PARAMETERS

The simplest types of functions are functions that just call a bit of code. Like this..


And in the main loop, you would call that function like this:


Or even...


Although I prefer the former, as it looks neater and more recognisable within the code

Functions can also contains functions within them, like this:


I usually split my main loop into 2 functions, UpdateGame() and RenderGame(), each containing functions as above.

Look at the source code in the zip for more

Now lets move onto a whole new world...Functions with Parameters

FUNCTIONS WITH PARAMETERS

Let's go back to the first function we looked at


It's possible to do this


a$ is a string variable, so you would call the function like this:


And it would print "This is cool!"

It happens like this. a$ is a variable which you declare when you declare the function, as we did "This is cool" when we declared the myfunct function. As you get into longer and longer functions with parameters, it will save you a lot of code.

Here's another part of functions...

RETURNS

Returns immediately go back to the code where you called the function and returns a value. If you don't understand, take a look at this:


That function multiplies the first number by the second number. You would call that function like this


The variable 'multipliednum' becomes 10 (the product of 2 and 5). To return float numbers from a function, add a # onto the name of the function, like this:


The same for string variables.

Lets take a look at some functions in action

LOOKING AT FUNCTIONS

Here's my input function. I created this because I found out that the input function that comes with Blitz only works on the front buffer


There are 7 parameters to this function.

The 'x' and 'y' are the co-ordinates. The 'que$' variable is the question the user wants to ask, the 'ans$' variable is a blank string variable which the user returns, and the r, g and b variables are colour of the text that the user wants


Sets the color to whatever the r,g,b was in the function


Returns the key's ascii number


If the GetKey's value is between 32 and 122 (a displayable character)


Turn it into a character and and add it to the ans string


Checks for a backspace key, and removes the characters accordingly


Displays the que and ans string


If the user presses Enter, it will return the answer string to the calling code


Ends the function

What we've learnt in this Tutorial

Calling functions without parameters

Containing functions within functions

Calling functions with parameters

Returning values from a function

CONCLUSION

That's it for this tutorial. If you have any comments or suggestions please e-mail me

Cya,

CloudNine :D


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.