current stable:
0.99.6
unstable:
cvs (0.99.7)
General
  Home / News
  About
  Contact
  The Team

Obtaining
  Download
  Source Tarball
  CVS Web View
  Misc. Files

Documentation
  Introduction
The Manual
  Download [html]
  Download [pdf]
  View Online
API
  Download [html]
  View Online
Resources
  Script Examples

Developer
  Introduction
Developer Guide
  Download [html]
  Download [pdf]
  View Online
Ferite C API
  Download [html]
  View Online




Open Source Approved

SourceForge Logo
KwMap.net - browse the Keyword Map of ferite.org

[previous] Control Structures[up][toc]Classes and Objects (and references) [next]


Functions

Functions 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:

    function function_name( parameter declarations ){
    	variable declarations
    	statements
    }
    				

  • function_name -- This is the name of the function to be called e.g. Print, Open.

  • parameter declarations -- This is the signature of the arguments that can be passed to the function, and these are of the following form: <type> <name> (a comma seperated list)

  • variable declarations -- See section Variables

  • statements -- See section Statements

Example:

    /*
      This function will add the string "foo" onto the end of the string it has been given and then
      return it.
    */
    function foo( string bar ) {
    	bar += "foo";
    	return bar;
    }
    				

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.

    uses "array", "console";
    
    function test( string fmt, ... ){
       number i = 0;
       array fncArgs = getArgs();
       
       Console.println( "test() called with ${ Array.size(fncArgs) } args" );
       Console.println( fmt );
       
       for( i = 0; i < Array.size(fncArgs); i++ ){
    	  Console.println( "Arg[$i]: ${ fncArgs[i] }" );
       }
    }
    
    test( "nice" );
    test( "nice", "two", "pretty" );				
    				

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:

    return someValue * 10;
    return 0;
    return "Hello World";
    				

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.

    uses "console";
    
    function print( number n ){
    	Console.println( "Number: $n" );
    }
    
    function print( string s ){
    	Console.println( "String: $s" );
    }
    
    print( 10 );
    print( "Hello World" );
    				

The above code declares two functions with the name print. If the script is run the following output would occur:

    Number: 10
    String: Hello World
    				

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]:

    uses "console";
    
    function print( number n )
    	Console.println( "Number: $n" );
    
    function print( string s )
    	Console.println( "String: $s" );
    
    print( 10 );
    print( "Hello World" );
    				


[previous] Control Structures[up][toc]Classes and Objects (and references) [next]
ferite et al © 2000-2004, Chris Ross