current stable: 0.99.6 unstable: cvs (0.99.7) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
FunctionsFunctions are made up of variable declarations and statements [as described previously]. Each statement is terminated by means of a ; - as mentioned before. Functions are declared as follows:
Example:
Functions provide an easy way of grouping statements together to perform a task. It must be noted that all variables must be declared before any other code - it is not possible to declare variables within the other statements - it will cause a compile time error. Variable Argument Functions Functions can take a varaible number of arguments by placing a ... at the end of the argument list. An array can be obtained with all the variables passed to the function by using the function call getArgs(). Example: The following program listing shows how to access the array and make use of it.
Returning A Value If there is not an explicit return statement then the function will return a void variable. To return a variable it is as simple as using the return keyword: Example:
Function Overloading There are times when you wish to have the same operation applied to different data types, for example, an print method where you wish to handle various different types and/or number of arguments. Ferite provides a function overloading mechanism to combat this which allows you to write a set of functions all with the same name but with different parameters. When the program is run - ferite will automatically choose the best function for the job.
The above code declares two functions with the name print. If the script is run the following output would occur:
One Line Functions One last and final thing that should be noted about functions is that if you only have a one line function - you do not need to include the braces around the code. This is used to make things cleaner and tidier. For example, the above script written using this feature would looke like this [note the lack of braces]:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
ferite et al © 2000-2004, Chris Ross |