Functions Section

All functions used in a Silk Performer test script are defined in the functions section. Functions should be used to give your script more structure. The syntax of functions is identical to the syntax of transactions, except for the items mentioned below.
  • Functions are defined using the keyword dclfunc
  • One script may include more than one functions section, although each function must be defined before it is used.
  • You can specify an option optional for the parameters of external and internal functions. This option indicates that the parameter is optional. If an optional parameter has not been passed in, its value will be 0 on entry to the function. For the data types CURSOR, SQL and FORM a value of -1 is passed on entry to the function.
    Note: All parameters following the first optional parameter for a function must also be optional.

We distinguish between external and internal functions. External functions are the BDL functions that are readily available for use, like for example WebPageUrl. Internal functions are functions that are implemented within your script. Random variables can be used as parameters of internal and external functions. Both external and internal functions can also be used as parameters of other external and internal functions.

Syntax

FunctionSection = "DCLFUNC" { Function }.

Function        = [ @DEPRECATED [ "(" NewFuncName  ")" ] ] "FUNCTION" ident [ "(" TParList ")" ]
                  [ ":" RType ] [FuncAttribute]
                  [ "CONST" ConstDecl ]
                  [ "VAR" VarDecl ]
                  "BEGIN" StatSeq "END" [ ident ] ";".

TParList        = TParam { ";" TParam }.

TParam          = IdentList ":" PType PModifier.

PType           = ["ARRAY" | "LIST" "OF" ] "STRING" "(" number ")"
                  | ["ARRAY" | "LIST" "OF" ] "NUMBER"
                  | ["ARRAY" | "LIST" "OF" ] "BOOLEAN"
                  | ["ARRAY" | "LIST" "OF" ] "FLOAT"
                  | "SQL"
                  | "FORM".

PModifier       = "allownull" | "optional" [ ":=" (signum | float | boolean | ident)].

RType           = "STRING" "(" number ")"
                  | "NUMBER"
                  | "BOOLEAN"
                  | "FLOAT".

FuncAttribute   = "<" FuncAttributeValue ">"
Section Description
ident The name of the function.
ConstDecl The declarations of the function constants.
VarDecl The declarations of the function variables.
StatSeq The statements of the function.
FuncAttributeValue Specifies attributes for the function. The following options are valid:
  • API_FUNCTION: The function behaves like an API function. If an error is raised within this function, the line number and file of the caller is reported.
  • ASYNC_CALLBACK_FUNCTION: The function is used as callback function for the async replay response handling. Whenever response data of an async call is available, the function is called by the replay engine. Functions specified as ASYNC_CALLBACK_FUNCTION must not have parameters.
  • BDL_FUNCTION: (Default) The function behaves like an ordinary BDL function. If an error is raised within this function, the line number and file of where the error occurred is reported.

Example

benchmark BenchmarkName
use "kernel.bdh"

const 
  MAX_ARRAY := 5; 
// Definition of global variables: string, number, float, boolean, array
var 
  sa1 : array [MAX_ARRAY] of string init "string1", "string2",
"string3", "string4", "string5" ;
na1 : array [MAX_ARRAY] of number init 1,2,3,4,5 ; 

dclfunc 
  function foo1 (psa1 : array of string; pna1: array of number; len : 
number):number 
  var i : number; 
  begin 
    for i := 1 to len do 
      Print(psa1[i]); 
      Print(string(pna1[i])); 
    end; 
  end foo1;
 
  function Func1(p1: number; s: number optional): number 
  var 
    i, j: number; 
  begin 
    if s = 0 then 
      j := 1 
    else 
      j := s 
    end; 
    for i := 1 to p1 do 
      j := j*i; 
    end; 
    Func1 := j; 
  end Func1; 

  function Func2(p1: number) 
  var 
    i, j: number; 
  begin 
    j := 1; 
    for i := 1 to p1 do 
      j := j*i; 
    end; 
    p1 := j; 
  end Func2;
 
  function Func3 
  begin
    write( "TestFunc was sucessfull"); 
  end Func3; 

dcluser 
  user 
    VirtUser 
  transactions 
    TestFunc : 1;  // Transactions 

dcltrans 
  transaction TestFunc 
  var 
    l, m: number; 
  begin 
    foo1(sa1,na1,MAX_ARRAY); 
    l := Func1(10); 
    m := Func1(l); 
    Func2(l); 
    Func3(); 
  end TestFunc;