VBScript Function
What is a Function?
A function is a group of reusable code which can be called anywhere in your program. This eliminates the
need of writing same code over and over again. This will enable programmers to divide a big program into a number of small and manageable functions.
Apart from inbuilt Functions, VBScript allows us to write user-defined functions as well. This section will explain you how to write your own functions in VBScript.
Function Definition
Before we use a function, we need to define that particular function. The most common way to define a function in VBScript is by using the Function keyword, followed by a unique function name and it may or may not carry a list of parameters and a statement with a End Function keyword, which indicates the end of the function. The basic syntax is shown below:
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function Functionname(parameter-list) statement 1 statement 2 statement 3
.......
statement n
End Function
</script>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function sayHello() msgbox("Hello there")
End Function
</script>
</body>
</html>
Calling a Function
To invoke a function somewhere later in the script, you would simple need to write the name of that function with the Call keyword.
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function sayHello() msgbox("Hello there")
End Function
Call sayHello()
</script>
</body>
</html>
Function Parameters
Till now, we have seen function without a parameter, but there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. The Functions are called using the Call Keyword.
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function sayHello(name, age)
msgbox( name & " is " & age & " years old.")
End Function
Call sayHello("Tutorials point", 7)
</script>
</body>
</html>
Returning a Value from a Function
A VBScript function can have an optional return statement. This is required if you want to return a value from a function.
For example, you can pass two numbers in a function and then you can expect from the function to return their multiplication in your calling program.
NOTE : A function can return multiple values separated by comma as an array assigned to the function name itself.
Example
This function takes two parameters and concatenates them and returns result in the calling program. In VBScript, the values are returned from a function using function name. In case if you want to return two or more values, then the function name is returned with an array of values. In the calling program, the result is stored in the result variable.
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function concatenate(first, last) Dim full
full = first & last
concatenate = full 'Returning the result to the function name itself
End Function
</script>
</body>
</html>
Now, we can call this function as follows:
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function concatenate(first, last) Dim full
full = first & last
concatenate = full 'Returning the result to the function name itself End Function
' Here is the usage of returning value from function. dim result
result = concatenate("Zara", "Ali") msgbox(result)
</script>
</body>
</html>
Field and Record Evaluation Formula
IMan supports two types of formulas:
Field formulae, which handle the data at individual field level and are used to set field values.
Record evaluation formulae apply to an entire record, with all its field and are used for conditional evaluation. .
These formulae can be written over multiple lines and support local variables.
Example 1
Multi-line Function with a Valid Return Value
Dim NetValue
NetValue = %INVOICE.GROSSVALUE - %INVOICE.TAXTOTAL
NetValue * -1
Example 2
Multi-line Function with an Invalid Return Value
Dim NetValue
NetValue = %INVOICE.GROSSVALUE - %INVOICE.TAXTOTAL
NetValue * -1
Dim CalculatedValue
Example 3
The return value is supposed to be NetValue, but the formula actually returns a Boolean comparison of the NetValue variable and the difference of the GROSSVALUE and TAXTOTAL fields.
Dim NetValue
NetValue = %INVOICE.GROSSVALUE - %INVOICE.TAXTOTAL
The correct formula should be:
Dim NetValue
NetValue = %INVOICE.GROSSVALUE - %INVOICE.TAXTOTAL
NetValue
In-line Formula
In-line formulae are limited to a single line which must evaluate to a result. These formulae are used primarily in transform set-up fields to dynamically generate a specific entry, such as a file name in a Writer transform.
Field References
Field references allow data in the dataset to be referenced and manipulated.
Field references have two versions of syntax:
%[FIELD NAME]
or
%[TRANSACTIONTYPE.FIELD NAME]
Where:
- %
- Identifies the following statement as a field reference.
- TRANSACTIONID
- The transaction being referenced.
- The transaction type is required only when it cannot be inferred from the context, such as in in-line formulae and Expando fields.
- FIELDNAME
- The name of the field being referenced.
If there are spaces or special characters within the field name/transaction Id, enclose the field references in square brackets.