Scripting in StreamServe > Function files > Functions with arguments

Functions with arguments
Some functions need arguments in the input. If you do not declare arguments for such a function you can use #1 to retrieve the first argument in the input, #2 to retrieve the second argument, and so on. For example:
func FileNameString ()
{
$namestring = #1 + "_" + #2;
return $namestring;
}
You can also declare the arguments for a function as local variables. A function that includes argument declarations has the following form:
func functionName([type name,])
{
[statements]
[return value;]
}
Example 32
func myfunc(str firstname, str lastname, num age)
{
if (age > 29)
return firstname + " " + lastname;
else
return 0;
}
 
The arguments declared in the function and variables declared in the function body are local to the scope of the function call. This makes custom functions more portable because there is no risk of overwriting global variables. The function is also more useful as the expected arguments are clearly defined in the function rather than hidden within statements in the function body.
The arguments declared in the function have values that should not be altered in the function, i.e. they should only be read from and not written to. For example, the following function will cause an error:
func myfunc(str firstname, str lastname, num age)
{
clear(firstname);
if (age > 29)
return lastname;
else
return 0;
}
You can still use the #-syntax to read the arguments to a function when argument variables are declared. For example:
func myfunc(str firstname, str lastname, num age)
{
if (age > 29)
return #1 + " " + #2;
else
return 0;
}
Comments
Argument variables cannot be initialized in the function declaration. If you need to initialize a variable you must do it in the function body.
Examples (arguments declared)
Example 33
This function uses two arguments to create the string "customer name_invoice number":
func FileNameString(str yourref, str invno)
{
str namestring = yourref + "_" + invno;
return namestring;
}
Function call (global variable):
$nameNumber = FileNameString(&yourReference, &invoiceNumber);
Function call (local variable):
{
str nameNumber = FileNameString(&yourReference, &invoiceNumber);
}
 
Example 34
This function changes the date format mm/dd/yyyy to yyyy-mm-dd:
func ChangeDateFormat(str date)
{
str month = date(1,2); //Character 1 and 2 in input string
str day = date(4,2); //Character 4 and 5 in input string
str year = date(7,4); //Character 7 to 10 in input string
str newformat = year + "-" + month + "-" + day;
return newformat;
}
Function call (global variable):
$dateFormatSWE = ChangeDateFormat(&invoiceDate);
Function call (local variable):
{
num dateFormatSWE = ChangeDateFormat(&invoiceDate);
}
 
Examples (arguments not declared)
Example 35
This function uses two arguments to create the string "customer name_invoice number":
func FileNameString()
{
$namestring = #1 + "_" + #2;
return $namestring;
}
Function call (global variable):
$nameNumber = FileNameString(&yourReference, &invoiceNumber);
Function call (local variable):
{
str nameNumber = FileNameString(&yourReference, &invoiceNumber);
}
 
Example 36
This function changes the date format mm/dd/yyyy to yyyy-mm-dd:
func ChangeDateFormat()
{
$month = #1(1,2); //Character 1 and 2 in input string
$day = #1(4,2); //Character 4 and 5 in input string
$year = #1(7,4); //Character 7 to 10 in input string
$newformat = $year + "-" + $month + "-" + $day;
return $newformat;
}
Function call (global variable):
$dateFormatSWE = ChangeDateFormat(&invoiceDate);
Function call (local variable):
{
num dateFormatSWE = ChangeDateFormat(&invoiceDate);
}
 
 
 
OpenText StreamServe 5.6.2 Updated: 2018-01-26