• Tutorials

  • Home
  • GPDL Functions

    DungeonCraft Help Home

    Functions are pieces of code that operate on the parameters supplied by the 'calling' function and return a single value to that function. An example of a system function is:

       $RANDOM ( n );

    This function receives a single parameter (that it calls n) and returns a single value, a number between 0 and n-1.

    You define your own functions as follows:

      $FUNC  square ( n )
      {
        $RETURN $TIMES ( n, n );
      } square ;

    You will immediately notice four interesting things in this example:

    No return value type specified

    The keyword '$FUNC' precedes the function's name

    No datatype is specified for the parameter 'n'

    The function's name is repeated after the closing curly brace.

    No return value type.....This is because variables can only be of one type. It would be a bit redundant to say that it is a string.

    The keyword '$FUNC'.....This is for no really good reason. It makes things easier to compile. It makes typos easier to isolate because of the added redundancy in the syntax.

    No datatype for the parameter. Again, there is only one datatype. String. Period.

    The function's name following the definition.....This serves no purpose other than to add redundancy to the syntax. This makes it easier to find missing braces and other simple errors. Otherwise, especially because of the nested function capability, there are sometimes dozens of lines between the error and the discovery of the error.

    In order for a function to be referenced directly at runtime it must also be declared as $PUBLIC. For example:

       $PUBLIC $FUNC sayHello ( )
       {
          $SAY ("Hello");
       } sayHello ;

    See GPDL Pragma Directives for another way to declare functions public.

    Every proper function should finish its work someday and return to its caller. The return is accomplished with a $RETURN statement. All functions return a value. If a value other than "" is to be returned then that value is listed on the $RETURN statement. Examples:

    $RETURN;   The value returned is the empty string.
    $RETURN "Samual" ;
    $RETURN $nPLUS($XXX(39), 3);

    At the end of a function definition the compiler puts a $RETURN that you cannot see.