The sub..end sub control statement defines a subroutine which can be called upon from a call statement.
Sub name [ ( paramlist )] statements end sub
Arguments are copied into the subroutine and, if the corresponding actual parameter in the call statement is a variable name, copied back out again upon exiting the subroutine.
If a subroutine has more formal parameters than actual parameters passed by a call statement, the extra parameters will be initialized to NULL and can be used as local variables within the subroutine.
|name||The name of the subroutine.|
|paramlist||A comma separated list of variable names for the formal parameters of the subroutine. These can be used as any variable inside the subroutine.|
|statements||Any group of one or more Qlik Sense script statements.|
Since the sub statement is a control statement and as such is ended with either a semicolon or end-of-line, each of its two clauses (sub and end sub) must not cross a line boundary.
When you define a subroutine with Sub..end sub inside a control statement, for example if..then, you can only call the subroutine from within the same control statement.
Sub INCR (I,J)
I = I + 1
Exit Sub when I < 10
J = J + 1
Call INCR (X,Y)
Example 2: - parameter transfer
Sub ParTrans (A,B,C)
Call ParTrans (A, (X+1)*2)
The result of the above will be that locally, inside the subroutine, A will be initialized to 1, B will be initialized to 4 and C will be initialized to NULL.
When exiting the subroutine, the global variable A will get 2 as value (copied back from subroutine). The second actual parameter “(X+1)*2” will not be copied back since it is not a variable. Finally, the global variable C will not be affected by the subroutine call.