Modular programming refers to the idea that programs are easier to read, to write, to debug, and to maintain if they are divided into smaller sub-programs. The benefits of modular programming are several. First of all, it makes our programs easier to write because individual components can be independently written and tested. Second, it makes the “main’’ part of the code easier to read since long code sections are replaced with simple functions (whose internal code is hidden in another file). This also makes our programs simpler to modify. Third, individual components can be reused in other programs. For example, suppose you write a program that accepts data from the keyboard and calculates the average, standard deviation and so on. If the part of the program that calculates the standard deviation is contained inside a separate function, you could reuse that function in another program that needed to calculate the standard deviation.
Even though the simple program structure works well for simple example, it is counter-productive for longer programs, leading to lack of clarity and slowing code maintenance and modification. Each well-defined task in a program should be placed in its own program unit. Here you will execute the tasks in program units called subroutines. The main program simply acts as an outline or driver, triggering execution of the program units that accomplish the tasks. Figure 3.1 shows the modular programming approach. This chapter describes modular programming approach in LabVIEW software.
The power of LabVIEW lies in the hierarchical nature of the VI. After you create a VI, you can use it on the block diagram of another VI. There is no limit on the number of layers in the hierarchy. Modular programming helps manage changes and debug the block diagram quickly. Modularity defines the degree to which your VI is composed of discrete components such that a change to one component has minimal impact on other components. These components are called modules or subVIs. Modularity increases the readability and reusability of your VIs. As you create VIs, you might find that you perform a certain operation frequently. Consider using subVIs or loops to perform that operation repetitively. In Figure 3.2 the block diagram contains two identical operations to convert temperature in Celsius to Fahrenheit inside and outside the loop. You can create a subVI that performs that operation and call the subVI twice. The example in Figure 3.3 calls this temperature VI as a subVI twice on its block diagram and functions the same as the previous block diagram. You also can reuse the subVI in other VIs.
A VI within another VI is called a subVI. A subVI corresponds to a subroutine in text-based programming languages. You can reuse a subVI in other VIs. If you use a VI as a subVI, the icon identifies the subVI when it is called from the block diagram of another VI. Windows Explorer also uses the icon to identify the .vi file if you enable this feature. The steps to create a subVI is, first build a VI front panel and block diagram, next build the icon and the connector pane and then you can use the VI as a subVI.
Free Demo for Corporate & Online Trainings.