Previous SectionFlow Control
Document HomeContents
LibrariesNext Section

Procedures and Functions

A feature of OVM not found in the original Ozterm script language is the ability to define your own procedures (commands) and functions. OVM introduces the keywords procedure, function, and byref to implement this ability.

Procedures and functions must be defined before they can be used. That is, that must appear in the script file before where they are called.

Note. OVM does not currently support recursion. That is, a procedure or function cannot call itself.

The procedure keyword allows you to define your own command with or without parameters that can be called just like any inbuilt command.

! This procedure displays the requested number of lines of ":-)"
@procedure ManyHappyReturns(integer howmany)
@begin
    @for integer i = 1 to howmany
        @comment(":-)")
@end

@ManyHappyReturns(10)        ! Display 10 :-) lines

The keyword function allows you to define your own function that returns one of the fundemental variable types (ie integer, boolean, string).

! This function will return the factorial of numbers 0 to 12
! Eg.   Factorial(4) = 4 * 3 * 2 * 1 = 24
! Note. Factorial(0) = 1
! Note. Negative numbers will set the error flag
! Note. Factorials greater than 12 are too large for us to compute
@function integer Factorial(integer number)
@begin
    ! Check that the number is within the range we can compute
    @if number >= 0 and number <= 12 then
    @begin
        @integer answer = 1
        @for integer i = number downto 1
            @answer = answer * i
        @set factorial = answer
    @end
    @else
    @begin
        @set Factorial = 0
        @seterrorflag(true)
        @return
    @end
@end

! Display the factorials of 0 to 12
@for integer j = 0 to 12
    @comment("Factorial ",j," = ",Factorial(j))

The return value of a function can only be set from within the function by assigning a value to the function’s name. If you do not assign a value during a particular call then the last assigned value will be returned. This behaviour may change in future versions of OVM.

To specifiy multiple parameters of the same type separate the parameter names by commas.

@procedure fred(integer a,b,c)

To change parameter types use a semicolon followed by the new type name.

@procedure mary(integer a,b,c; string d,e; boolean f)

To pass a parameter by reference use the byref keyword. Passing by reference allows you to modify the value of the actual parameter passed to the procedure or function (by default parameters are passed by value which means the value of the argument passed to the procedure or function is copied to the corresponding parameter).

! a is passed by reference
! b is passed by value (default)
@procedure byrefexample(byref integer a; integer b)

The maximum number of parameters a user defined procedure or function can have is 10.

@procedure maxparams(integer a,b,c,d,e,f,g,h,i,j)

Note that arrays must be passed by reference (ie you must use the byref keyword), and that you only specify the number of dimensions using commas (but not the bounds) of the arrays you wish to pass.

@procedure useanarray(byref integer array3d[,,])
@begin
    ! Do something useful with the array here
@end

Just use the array name when calling the procedure or function.

@integer fred[10,10,10]
@useanarray(fred)

At runtime the bounds of the array you pass are still checked as if you were using a normal array.

You can call a user defined function or system defined function with the @call command if you do not want to assign or check the functions return value.

To force an exit from a procedure or function early use the @return command.

@procedure myproc()
@begin
    @return
    ! The line below cannot be reached
    @comment("You can't get to me")
@end


Previous SectionFlow Control
Document HomeContents
LibrariesNext Section



This publication has been prepared and written by Telstra Corporation Limited (ACN 051 775 556), and is copyright. Other than for the purposes of and subject to the conditions prescribed under the Copyright Act, no part of it may in any form or by any means (electronic, mechanical, microcopying, photocopying, recording or otherwise) be reproduced, stored in a retrieval system or transmitted without prior written permission from the document controller. Product or company names are trademarks or registered trademarks of their respective holders.

Note for non-Telstra readers: The contents of this publication are subject to change without notice. All efforts have been made to ensure the accuracy of this publication. Notwithstanding, Telstra Corporation Limited does not assume responsibility for any errors nor for any consequences arising from any errors in this publication.