faq | docs | tutorials | guide | reference | contact | home  

TUTORIAL - FUNCTIONS Previous | Next | Tutorial Home

The FGL language provides robust support for functions and procedures. Functions and procedures operate in much the same way except that function must return a value and procedures can not return a value (the rest of this tutorial focuses on functions, but much of this applies to procedures as well).

The FGL environment provides a number of predefined functions embedded directly into the FifthGen Engine or available through external FGL libraries or other binaries (such as Active X controls, Java, and .NET assemblies).

FGL supports private and public functions, embedded functions within Active Pages, and binary libraries including functions. Functions can be passed any type of information as parameters, or no parameters, and functions fully support variable number and types of parameters. FGL also supports optional default values for missing parameters.

Simple Function Example:
<[
    FUNCTION FormatName( fname, lname )
        return( fname + " " + lname )
    END    
]>
<html>
<head>
 <title>Simple function example</title>
</head>
<body text=black bgcolor=white>
<[
    ! FormatName( "Steve", "Repetti" )
]>
</body>
</html>

And here is a variant of the previous example using variable parameters:

Variable Parameter Function Example:
<[
    FUNCTION FormatName( fname, lname, IsLastFirst )
        
        if ( pcount( ) == 1 )
            return( fname )
        end
        
        IsLastFirst = type( IsLastFirst ) == "N" ? IsLastFirst : 0
        
        if ( IsLastFirst )
            str = lname + ", " + fname
        else
            str = fname + " " + lname
        end
        
        return( str )
    END    
]>
<html>
<head>
 <title>Variable Parameter Function Example</title>
</head>
<body text=black bgcolor=white>
<[
    fname = "Angela"
    lname = "Repetti"
    ! FormatName( fname, lname ) + "<br>"
    ! FormatName( fname, lname, 0 ) + "<br>"
    ! FormatName( fname, lname, 1 ) + "<br>"
    ! FormatName( "Angela L. Repetti" )
]>
</body>
</html>

Function names are not case sensitive and functions used in an Active Page must be at the top of the page after library statements and before any other FGL statements excluding other functions and classes, for example:

Function Syntax in an Active Page:
<[
    // library statements
    // functions and classes
    // regular FGL statements
]>
<html>
<head>
 <title>FGL Function Syntax</title>
</head>
<body text=black bgcolor=white>
</body>
</html>

The MAIN function is reserved in Active Pages and must not be used. By default, any FGL statements not otherwise contained within a function, procedure, or class are automatically considered the “implied” main function. Standalone EXEs can optionally have a main function, though if none is specified then an implied main is created similar to that as in an Active Page.

Default parameter values can be assigned directly and then accessed via the function call. If the parameter is not passed then the default value is used, otherwise the passed parameter is used within the function, as in:

Default Function Parameters example:
<[
    FUNCTION displaydate( month = date("MM"), day = date("DD"), year = date("YYYY") )
        return( month + "-" + day + "-" + year )
    END
]>
<html>
<head>
 <title>Default Parameter Passing example</title>
</head>
<body text=black bgcolor=white>
<[
    ! displaydate( ) + "<br>"
    ! displaydate( ,"01" ) + "<br>"
    ! displaydate( ,,"1976" ) + "<br>"
]>
</body>
</html>

#####