|
8.1 About functions
You have already used one function in all your programs.
It's the main function, that is the start point of our programs.
So why use more functions?
Imagine you have to do some calculation that depends on some input data. And you
have to do it at different points in your program and with differnet values for
the input data. Of course you can write the code each time you need it, but what
if it's a complex calculation or if you have to use it pretty often? And what
if later, you want to adjust it a little bit? You will have to go through all
your program and that can be really difficult.
So, to avoid these ugly situations, you group some parts of your program into functions.
To declare a function, you must write the key word func
followed by it's name, the function parameters
names inside brackets ( ), separated by commas, and then
the instructions block enclosed in braces { }.
func name( parameter1, parameter2, ... )
{
instructions
}
If a function has no parameters, like the main function, then nothing will be written inside the brackets.
You can't declare a function inside other function and you can't declare two functions with the same name.
Choosing names for functions is similar to choosing names for variables.
In a function, you can declare a variable with the same name as another variable from another function.
They will not be mistaken, because such variables are considered local variables,
each in it's function.
To use a function, you must call it, from another function. Meaning you expect
the processor to jump to the called function, execute all it's instructions
starting with the first one, and then return and continue the execution from where
it was, after the call instruction.
To call a function, just write it's name and give values for the expected parameters inside brackets.
Let's have an example (copy all the following code into the "program.txt" file).
The function AddTwoNumbers expects to receive two parametsrs, named a and b. Those
parameters are like local variables and can be usable only inside the function. Their values
come from the caller function and they are 1 and 2 for the first call, and 10
and 20 for the second.
The parameters (also called arguments) types must match their purpose in the function where they are send to.
If a function expects an numeric parameter, sending a table instead will produce a bad argument type error.
It's like when you must add some numbers that someone dictates you on the phone,
and suddenly he starts telling you some people's names instead the numbers you're expecting.
You will not be able to add them to your sum, would you?
8.2 Returned values
As said before, after finishing to execute all instructions from a function, the
processor returns to the caller function, from where it left.
You can force the processor to return before it reaches the last instruction
from the current function, using the return instruction.
Using return in the main function will cause the program to
exit right at that point, since there's no other higher function to return to.
Another smart thing functions can do, is to return a result to the caller function.To
do so, use the return instruction, followed by the
constant value or the variable who's value you want to return. To use such a result, in the caller
function you must store it in a variable, use it in an expresion, or give it to another function as a parameter.
Since the returned value have it's own type, we say that the function returns
that specific type and it can be received only in a variable of the same type.
Here is an example of a function with two parameters that returns their sum.
As you see above, before a function call, all parameters are evaluated,
and only then they are sent to the function.
GS9 language supports returning multiple values that can be used in a multiple assign operation.
The returned values are matched with the variables in the assign operation providing additional ZERO values if needed
or discarding unneeded values.
Take great care when calling functions from themselfs.
It's called recursion and it's a pretty useful but quite advanced technique,
that if not used correctly, may lead to a program crash named stack overflow.
Make sure you always have a returning condition, before the self call. Here is a bad example:
The program above will list numbers starting with 1 and, unless corrected, it
it will quickly produce the stack overflow crash.
8.3 More about parameters
In GS9 all parameters are sent as "duplicates" except for strings and tables.
This means that if you send an numeric parameter to a function and inside that function the parameter is changed,
this will not affect the original value.
However if you send a table as a parameter and change it inside the function, the original table will change too.
This is done for performance purposes, to prevent duplicating the whole table each time it's sent to a function.
Same thing with strings, though since you can't actually change their content this is not very obvious.
Here is an example of changing content of a table.
You can also handle situations when parameters can come with different types, using the typeof operator,
or you can even have some missing paramters, that will be filled in as zeros.
Here is an example of handling different types of parameters:
|
|