Function?
Blitz3D Forums/Blitz3D Beginners Area/Function?
| ||
Hmmm... Can't seem to figure out how to use functions. Could someone please explain it for me? |
| ||
Do you have a specific question, like what something means? Have you read the manual? Create a function: Function duh() ...put code here... End Function Call it in your main program: duh() |
| ||
Ok a function is really quite simple. It stores code that can be accesed from anywhere in the program yo are writing. example below. while not keyhit ( 1 ) cls myfunction() <<<<This makes a call to myfunction() flip wend end Function myfunction() <<<This is the function being called..:) print "Hello, Im inside a function called myfunction()" end function |
| ||
Functions make code managable. You can place them in other files(includes) and some editors can fold them(hide them). You can for instance place each part of a program/game inside a function. Like below. intro() game() outro() The entire game would be called in these 3 lines of code, just by using functions. (Not that a game could be made in 3 lines) Some extra info. Variables inside functions are not global. Every variable will be freed/forgotten when the function is finished. To return a value from a function you should do : print addition(10,10) end function addition(a,b) return a+b end function Calling the 'Return' alone will exit the function at the point of calling. Arrays/types and global variables can be accessed and modified from inside a function. You can not create global variables/arrays/types inside functions. Functions can have predefined arguments. Function addition(a=0,b=0,c=0,d=0) return a+b+c+d end function Functions can return integers(this is default) Strings(text), or float values(0.42234 ect..) Function mergestring$(a$,b$) return a$+b$ End Function Function Addfloat#(a#,b#) return a#+b# End function You can create Blitz arrays inside functions. They do not have error checking, so do not use these until you know how they work. Function dostuff(a,b,c,d) Local stuff[3] stuff[0] = a stuff[1] = b stuff[2] = c stuff[3] = d ;and ect... End function This is all I can come up with at this time. Hit the documentation and look at examples is the best advice i can give. |
| ||
most important thing with Functions is when you use a specific Part of Code multiple times then it's better to write a function that can be called as many times as you want. I guess it was said: Variables n the Main Program (means outside any function) must be defined as Globals if you want to be able to access them inside a Function. Also all Variables you use inside the Function are only Local Variables (unless they are defined as Globals outside the functions), this means their Values are "forgotten" as soon as you end a function. So to hand over some Variables to a function you can use Globals, or Parameters in the brackets, here the Mouse Position: setbuffer frontbuffer() while not keydown(1) if mousedown(1) drawmouse(mousex(), mousey()) ; << the function call endif wend end function drawmouse(x,y) plot x,y end function |
| ||
Thanks. I know it was probably a really stupid question, sorry. |
| ||
no problem. Docs usually forget to explain fundamental stuff clearly. |
| ||
There are no such thing as stupid questions, just stupid people. Honestly, just kidding. As I was saying in a previous post, some of us forget what it was like when we first started out and how even the simplest stuff was hard to grasp. So ask away! |