Sometimes when you convert a basic program to blitz that contains Gosub, it's the quick and easy way not to touch it, so you don't have to worry about globals and locals.
In fact, sometimes the reason why a program doesn't work is when you forget to declare something to be global. The "strict" option in bmax is a solution that was missing in Blitz3D.
However. what's the diffrence between Gosub and Function?
Most important: Variables that are used inside of a function are locals, unless they are defined as globals outside the function.
example:
You see, although we call the function "job()" that will inclement a, it always remains the same. The reason is, threre are two A's, one in the main program (outside any function) and one in the function "job()". The a inside the function is local, this means the functoin cannot remember it's value. It is recreated whenever the function is called.
Now this is totally diffrent when you add the line "Global a" to he top of the example:
the function "job()" will now recognize that there is a global variable A and access it, instead of creating its own local variable.
In case you don't want the function "job()" to use the global a, but a local version, then you may define a as a local inside the function:
This last way is useful when you want to use a local variable (that is no longer used when the function is finished, but want to make sure not to disturb any other variable with the same name)
Basicly good programming style is to use as few as possible global variables in a program, Functions that are not using global variables are perfectly modular, this means, you can copy/paste one function to an other program without to worry about identic variable names.
Furthermore Functions take and return values.
It was described earlier in this thread. You don't have to use the Return Command in a function, if you omit it, the function will be finised at the "end function" command. There may be multiple Return commands in your function, eg:
One important bit is also this: depending on the return value you want to receive from a function (int, float or string) you need to call the function, eg:
a$=my_edited_string$("hello")
a#=my_ed_float#(3.12345)
a=my_int_or_something(358132134)
to make sure the function will be able to return the value.
BTW if you want the function to return multiple values, you may use globals or a bank.
In fact, sometimes the reason why a program doesn't work is when you forget to declare something to be global. The "strict" option in bmax is a solution that was missing in Blitz3D.
However. what's the diffrence between Gosub and Function?
Most important: Variables that are used inside of a function are locals, unless they are defined as globals outside the function.
example:
for i=0 to 10 job() print a next waitkey() function job() a=a+1 end function
You see, although we call the function "job()" that will inclement a, it always remains the same. The reason is, threre are two A's, one in the main program (outside any function) and one in the function "job()". The a inside the function is local, this means the functoin cannot remember it's value. It is recreated whenever the function is called.
Now this is totally diffrent when you add the line "Global a" to he top of the example:
global a for i=0 to 10 job() print a next waitkey() function job() a=a+1 end function
the function "job()" will now recognize that there is a global variable A and access it, instead of creating its own local variable.
In case you don't want the function "job()" to use the global a, but a local version, then you may define a as a local inside the function:
global a for i=0 to 10 job() print a next waitkey() function job() local a a=a+1 end function
This last way is useful when you want to use a local variable (that is no longer used when the function is finished, but want to make sure not to disturb any other variable with the same name)
Basicly good programming style is to use as few as possible global variables in a program, Functions that are not using global variables are perfectly modular, this means, you can copy/paste one function to an other program without to worry about identic variable names.
Furthermore Functions take and return values.
It was described earlier in this thread. You don't have to use the Return Command in a function, if you omit it, the function will be finised at the "end function" command. There may be multiple Return commands in your function, eg:
print "the max of 3 and 4 is " + max#(3,4) function max#(a#,b#) if a>b then return a else return b end function
One important bit is also this: depending on the return value you want to receive from a function (int, float or string) you need to call the function, eg:
a$=my_edited_string$("hello")
a#=my_ed_float#(3.12345)
a=my_int_or_something(358132134)
to make sure the function will be able to return the value.
BTW if you want the function to return multiple values, you may use globals or a bank.