Table of Contents

 

Function

A Function is a procedural artifact in TDL that contains a set of actions or statements executed in a defined order. It can accept input parameters and return a value.

Syntax

[Function : Function name]

Example

[Function: TSPL ActionFunction]

 

➥ Download the Function Samples

Sample File Attributes
Action Action
FetchObject FetchObject
ListVar List Variable
LocalFormula Local Formula
Object Object
Parameter Parameter
Return Return
StaticVariable Static Variable
Variable Variable

 

Attributes

Action lets you dynamically execute an action inside a function by specifying the Action keyword and/or its parameters as expressions, rather than fixed values.

The action itself, or its parameters, can be driven by expressions evaluated at runtime based on current data/conditions.
This works even for actions that don’t natively support expressions as parameters — the Action attribute effectively adds that capability.

In short: it makes normally-fixed actions dynamic, letting the function decide what to run and with what values at execution time.

Syntax

[Function : <Function Name>]

100 : Action : <Action Expression>

Example

[Function: TSPL ActionFunction]

100 : Action : Print Report

This example shows:

  • The use of the Action statement inside a function.
  • Here, the function executes the Print Report action when it is called.
  • Actions can be triggered dynamically using the Action keyword and their parameters can be passed as expressions.
  • This allows functions to control system actions programmatically, even for actions that do not normally support expressions.

Fetch Object enables the dynamic fetch of Objects at the function level retrieving a specific object (record/data item) during function execution, rather than through a fixed reference.

In short: it lets a function pull in the object it needs, on demand, while it’s running.

Syntax

[Function : <Function Name>]

Fetch Object : <Object Name> : <Method Name> : <Method Name>

Example

[Function: TSPL Smp Fetch Objects]

Fetch Object: Stock Item : $SICStockItem : $Name  ;;Using the Fetch Object attribute, the Stock Item Name is retrieved during function execution.

00 : LOG : “Objects fetched for Item – ” + $SICStockItem

 
  • This example demonstrates the use of the Fetch Object attribute at the function level.
  • During function execution, the Stock Item object is dynamically fetched using the identifier $SICStockItem, and its Name is retrieved.
  • The fetched object data is then used inside the function, where the Stock Item name is logged.
  • In short, Fetch Object allows a function to dynamically load and work with required objects during execution.

List Var is used to declare List Variables at the function scope a list that holds multiple values instead of just one, available only within that function.

Inline declaration you can declare the list directly with its data type and element count specified upfront, without needing a separate variable definition.

In short: it lets a function hold and work with a set of values (not just a single value) as a local, function-scoped list.

Syntax

[Function : <Function Name>]

ListVar : <Variable Name> : <Data Type>

Example

[Function: TSPL ListVariableFunction]

List Variable : List Var : String ;; List Variable is declared here

01: DISPLAY : TSPL ListVariableAttribute

This example shows:

  • How a List Variable is used to store multiple values inside a function.
  • Here, group names are collected and added to the list one by one using a counter as the key.
  • List Variables help store and manage a set of values during function execution.

Local Formula is used to specify a formula local to the function block available only within that function, not affecting or accessible from global formulas.

Syntax

[Function : <Function Name>]

LocalFormula : <Formula Name> : <expression>

Example

[Function: TSPL LocalFormulaFunction]

Variable: VarDateTime : String

LocalFormula : Date : $$String:$$MachineDate

LocalFormula : Time : $$String:$$MachineTime

100 : Set : VarDateTime : “Todays’s date is ” + @Date + ” \n and time is ” + @Time + ” hrs”

200 : Msg Box : “Alert” : ##VarDateTime

This example shows how Local Formula is used to calculate values within a function.

Here, Date and Time are defined as Local Formulas using the system’s machine date and time. These formulas are available only inside the function. Their values are used to build a message stored in VarDateTime, which is displayed using a message box.

Object by default, a function inherits the Object context of its caller. This can be overridden using the Object attribute in the function definition, which then becomes the function’s current object context.

Syntax

[Function : <Function Name>]

Object : <Object Name> : <Identifier>

Example

[Function: TSPL ObjectFunction]

Variable: VarClosingBal : Amount

Object : Ledger : “Cash” ;;The Object sets the current context to the Ledger and retrieves the methods for the Cash Ledger.

100 : Set : VarClosingBal : $ClosingBalance

200 : Msg Box : “Alert” : “Closing Balance for \n Ledger – Cash is ” + ($$String:##VarClosingBal) + “/-“

This example shows:

  • How the Object attribute controls the object context of a function.
  • Here, Object : Ledger : “Cash” explicitly sets the function’s context to the Cash Ledger, overriding the caller’s default context.
  • As a result, the function can directly access ledger fields like $ClosingBalance.
  • The closing balance is stored in a variable and displayed in a message box.
  • In short, the Object attribute forces the function to run in a specific object context so its fields and methods
    can be used directly.

Parameter is used to specify the parameters passed into a function these can then be referred to inside the function as variables.

You can specify a DataType and an optional default value for each parameter.

If a default value is provided, the caller can skip passing that parameter, and the default is used automatically.

Syntax

[Function : <Function Name>]

Parameter : <Variable Name> : <Data Type>

Example

[Function: TSPL ParameterFunction]

Parameter: VarStkItem : String

Variable : VarBatchNm : String

150 : Msg Box : “Check”: ##VarStkItem

100 : Walk Collection : TSPL StockItemBatchList

200 : If : $ItemName = ##VarStkItem

300 : Set: VarBatchNm : $BatchName

350 : Log: “Batch Name – ” + ##VarBatchNm

400 : End If

500 : End Walk

This example demonstrates:

  • The use of the Parameter attribute to pass input values to a function.
  • Here, VarStkItem is passed as a parameter and is accessed inside the function like a variable.
  • The function walks through the batch collection, compares the item name with the passed stock item, and retrieves the corresponding batch name.
  • Parameters can have a defined data type and an optional default value, allowing the caller to skip passing a value when a default is provided.

Return is used to return the flow of control back to the calling program, with or without a returned value.

When Return executes, the function’s execution terminates immediately.

The calling program then continues from the point where it called the function.

Syntax

[Function : <Function Name>]

Return : <Data Type>

Example

[Function: TSPL ReturnFunction]

Parameter: VarOpBalance : Amount

Parameter: VarClBalance : Amount

Variable : VarDiffAmount: Amount

Return : Amount

100 : Set : VarDiffAmount : ##VarClBalance – ##VarOpBalance

200 : Return: ##VarDiffAmount

This example shows:

  • The use of the ‘Return‘ in a function.
  • The function calculates the difference between the closing and opening balance and returns the result.
  • When Return is executed, the function stops running and control goes back to the calling program with the calculated value.

Static Variable is used to declare a variable as Static. Doing so persists its value across successive calls to the function, instead of resetting it each time the function runs.

Syntax

[Function : <Function Name>]

StaticVariable : <StaticVariable Name> : <Data Type> : <Initial Value>

Example

[Variable: CV Emp Static]

Variable : Name : String : “Radhe”

Variable : Age : Number : 10

[Function: TSPL StaticVariableFunction]

StaticVariable : CVEmpStatic

00 : SET : CVEmpStatic.Name : ##CVEmpStatic.Name + “-” + $$String:##CVEmpStatic.Age

10 : INCREMENT : CVEmpStatic.Age : 2

20 : LOG : “Name – ” + ##CVEmpStatic.Name + ” , Age – ” + $$String:##CVEmpStatic.Age

This example demonstrates the use of the StaticVariable attribute to maintain values across multiple function calls.

CVEmpStatic is declared as a static variable, so its values are not reset every time the function is executed.

Variable is used to declare simple variables at the function scope used inside the function for intermediate calculations.

Available only within that function.

If a datatype is specified, the variable is treated as an inline declaration.

Syntax

[Function : <Function Name>]

Variable : <Variable Name> : <Data Type>

Example

[Function: TSPL VariableFunction]

Variable: VarStkItem : String

Variable : VarBatchNm : String

150 : Msg Box : “Check”: ##VarStkItem

100 : Walk Collection : TSPL StockItemBatchList

200 : If : $ItemName = ##VarStkItem

300 : Set: VarBatchNm : $BatchName

350 : Log: “Batch Name – ” + ##VarBatchNm

400 : End If

500 : End Walk

  • This example demonstrates the use of the Variable attribute to declare function-level variables.
  • VarStkItem and VarBatchNm are simple variables used within the function for intermediate processing.
  • During execution, the function compares the stock item name and stores the matched batch name in VarBatchNm.
  • When a data type is specified, the variable is treated as an inline variable and is available only within the function scope.
Is this information useful?
YesNo
TallyHelpwhatsAppbanner
Is this information useful?
YesNo
TARA