Ooska,
Though Sven B's snippet function is correct, the function from it's beginning is useless the way it is set up. This is, unless the code to move the object with the implemented variables' values is going to be within the function itself, also. This is because the variables inside the function are local to that function, and will not effect any values outside of the function. This is true even if there are variables outside of the function that have the same exact names as the variables inside the function.
To fix this, the variables can be passed to the "outside world", by the use of the
return value. The return value, which can only be a single value(variable), is placed after
endfunction, with space between the two words. For example, let us say that you have a variable named
playerxpos which is withinside the function, and edited within the function. To pass
playerxpos to the outside world, the end of the function would be setup like so:
Remember, that only one value can be returned to the outside world. So, if you have two variables within the function, and you need return both of their values, you cannot. Only one variable can be returned with it's newly edited value.
We recieve this value from the function call by adding the function call to a variable. For example, let us say that we need to recieve the value within
playerxpos(the variable from within the function) into a variable named
newvalue. We do this just like when we pass a value from a variable to a variable, or when we fill a variable with a specific value, whatever it may be.
REM << notice that the function call is literally added to the variable
newvalue = playerposition()
These two examples, their methods together, will pass a variable that is within a function to a variable outside of a function.
However, what is the point of passing the same value, which can't be edited within runttime, to another variable, when you could simply do so at the start of a program, in your setup section? A value can also be passed to a function, to be used within a function, and then it's new value passed back to the outside world. This is mainly the whole purpose of a function. Which is, to pass values to a function, and the function to create a standard calulation on the value, and then pass the new value back to be used in the outside world.
To pass a value to a function, beit a variable or a pure value, you make use of
arguments, also called
perimeters. These arguments are placed within the brackets
() of the function call and the function itself. Arguments are simply variables or values, which are within the brackets. For example:
REM << the function call
do
newvalue = playerposition(newvalue,oldvalue#)
sync
loop
REM << the function;Notice how the values within the call are the same type of values as within the function areguments list
function playerposition(playerxpos,oldplayerxpos#)
playerxpos = oldplayerxpos# + 1
endfunction oldplayerxpos#
Pay close attention to how the perimeters are setup. The above is a runnable example. But how, seeing that the names within the function call's perimeter list differ from the names within the function's perimeter list? This is because names do not matter, not even the name of the return value, in relation to the variable in the outside world that has the return value passed to it from the function. Only the
types matter.
What do I mean by types? The types of values within the perimeter lists. There are three types of variables, real(with a decimal point), integer(whole numbers), and string(characters). These types
must match from perimeter to perimeter. Also, make sure that there is a comma seperating each value or variable with the argument lists, and that the argument lists are both setup the same. To clarify, if the function call's list includes an integer, then a real number, then an integer, then the function's list must be setup as so.
To use a value in a function's argument list within the function, use it by it's name that is within the argument list. If there is a variable within the argument list named
newtotal#, and you wish to add a value of 5 to it, take this example:
function newcalculation(newtotal#)
newtotal# = newtotal# + 5
endfunction newtotal#
To conclude, remember that all variables within a function are local to that function, and do not effect variables within the outside world, unless the values are passed to a variable within the outside world. Also, in liking, any variables within the outside world are local to the outside world and do not effect any variables within the function, unless passed to the function. This is why they are called local.
However, arrays are naturally
global, and can be edited within a function without having to pass their values to the function. Also, if a global variable is edited within a function, it will also be edited outside of the function as well, even without returning the value.

+NanoBrain+