What is a subroutine?

Official Content
This documentation is valid for:

It's a routine that is local to the object in which it is defined. That is to say, a block of code identified with a name. From there, it can be run within the object (and only there) using the Do command.

In this way, if we need to run the same block of code from several places of the object, it can be written once and invoked from the various locations.

Subroutines are defined using the Sub command.

Where are they created? In the objects that accept procedural programming in any of their sections: transactions, work, web, SD panels; work with for Smart devices: in the events section; procedures: in the source.

Features

  1. They don't support passing parameters: therefore, to exchange data we use variables which are global to the objects.
  2. They are not aware of the context (beyond variables and object parameters). This has some consequences:
  • In the programming defined in a subroutine, using "loose" attributes and expecting them to have the same value they had when making the call is not recommended (the subroutine can be invoked from several places within the object). Therefore, before calling the subroutine it is recommended that you assign these attributes to variables and use these variables in the subroutine.
  • If a subroutine contains For Each or New commands:
    • they won't be nested in For Each or New commands from where the subroutine was called, which means that there won't be any inferences or filters.
    • they won't be nested in implicit For Each commands corresponding to grids or the fixed part of web/work/SD panels.

Example

If in the source of a procedure or in the event of any of the above objects we have a For Each command that calls a subroutine:

For each
    …
    Do ‘Something’
    …
Endfor

and we have a subroutine defined:

Sub ‘Something’
   For each
      …
   Endfor
Endsub

Its For Each command will not be nested in the For Each command from where the subroutine was called. They will not be related, meaning that there won't be any automatic filters or inferences of any kind.

Note: the attributes used in an object will be global variables in the corresponding generated program. Therefore, if a certain attribute takes value in a certain section of an object, and later a subroutine that also assigns a value to the same attribute is called, when returning from the invoked subroutine and querying the attribute's value it will have the value assigned in the subroutine.


Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.