To make the function block more reusable and resilient to changes in the size of the button array, the fixed indexing (1 TO 10) can be replaced with dynamic bounds using the built-in LOWER_BOUND and UPPER_BOUND functions.
Instead of hardcoding the array size in the FB interface like this:
buttons : IN_OUT ARRAY[1..10] OF button
the array is declared as open-ended:

Open-ended Array
This allows the same FB to operate on arrays of any size, determined at the call location.
Inside the loop, we use the following logic:

Use of Lower_Bound & Upper_Bound on an open-ended array
Benefits of This Approach
- Flexibility: The function block can be reused with arrays of any length, without modification.
- Safety: Using LOWER_BOUND and UPPER_BOUND protects the CPU from potential runtime errors caused by index out-of-range issues.
- Scalability: If the system expands from 10 to 50 buttons, only the data block needs to be updated — no changes are required in the FB code.
This technique aligns with modern structured programming principles and is especially useful in large-scale or dynamic systems where component counts may vary.
If you found this helpful, please consider supporting with a small donation. Thank you!
Leave a Reply