Baanboard.com

Go Back   Baanboard.com

User login

Frontpage Sponsor

Main

Poll
For ERP LN feature pack upgrade, what method of install are you using?
Installation Wizard into existing VRC
38%
Installation Wizard into new VRC
41%
Manual into existing VRC
3%
Manual into new VRC
19%
Total votes: 37

Baanboard at LinkedIn


Reference Content

 
Function arguments
By patvdv at 26 Feb 2008 - 21:56

Function arguments

The arguments of a function are the variables declared in the function header. Arguments can be accessed only within the function.

Declaration, initialization, and scope

Point of declaration

In the function between the brackets { }.

Syntax of declaration

[ref|reference] <type> name

Note that array subscripts must be empty for reference arguments.

Initialization

At function call.

For value arguments (see below), the values of the function call are copied.

For reference arguments, the same variable (or the same memory area) is used as occupied by the variable submitted to the function call.

Scope (validity and time)

Only during function execution.

Value arguments

A value argument gets its value at the time of the function call and has its own memory space. For example:

 FUNCTION long dupl(long val)   | val is a value argument
 {
    RETURN( val * 2 )
 }
 FUNCTION MAIN()
 {
    message("%d", dupl(10))
        | the argument val gets value 10
 }

Reference argumentss

Reference arguments must be declared in the function header with the keyword REFERENCE or REF.

A reference argument has the same memory space as the variable used in the function call. Consequently, all changes made to a reference argument within the function block are also accessible after the function call in the variable used in the function call. If a reference argument has array subscripts, these must be empty in the declaration as the contents and size of the call variable is used. If strings or large arrays are used, it is preferable to use reference arguments instead of returning them with the return statement. This avoids copy actions.

Example 1

Note that in this example, the reference argument is empty in the declaration.

 FUNCTION LONG read_line( REF STRING buffer() )
 {
    IF ( . . . . ) THEN
        buffer = . . . . . . . . . . .
        RETURN( 1 )
    ELSE
        buffer = ""
        RETURN( 0 )
    ENDIF
 }

 FUNCTION MAIN()
 {
    STRING line(100)
    IF read_line(line) THEN
        . . . .
    ELSE
        message("No present line")
    ENDIF
              . . . .
 }

Example 2

This example uses a multi-dimensional array:

 FUNCTION VOID fill_array ( REF LONG array(,) )
 {
    array(1,1) = 10
    array(1,2) = 20
    ....
 }

 FUNCTION MAIN()
 {
    STRING      a(5,10)
    fill_array(a)
 }

Constant argumentss

Constant arguments must be declared with the keyword CONST in the function header. These are similar to reference arguments in that the same memory space is used for the constant argument and the variable used in the function call. However, with constant arguments, it is possible to use a string constant within the function call. Consequently, the value of the argument after the function call is not returned in the calling function.

You cannot change a constant argument within the function. Nor can you use a constant argument as reference in another function call. Only strings with one dimension can be used as constant arguments. The array subscript of the constant argument must be empty in the declaration because the size of the constant is used. For example:

 FUNCTION print_mess( CONST STRING mesg() )
 {
    IF NOT isspace(mesg) THEN
        message("Message: " & mesg)
      ENDIF
 }

 FUNCTION MAIN()
 {
    .....
    print_mess("This is a message")
    .....
 }

Varying number of arguments

It is possible to pass an (as yet) unspecified number of arguments to a function definition by replacing the actual arguments with '...'. The function call must contain the actual arguments required to execute the function. This enables you to use different arguments (from 0 to approximately 250) each time you call the function. This feature is useful, for example, if you wish to change some (not all) of the flags of a process, and you do not know beforehand precisely which flags you are going to change. See the arg() functions for information on using this feature.

Related topics



0
No votes yet


All times are GMT +2. The time now is 19:40.


©2001-2017 - Baanboard.com - Baanforums.com