How Does Recursion Work in LabVIEW?
Primary Software: LabVIEW Development Systems>>LabVIEW Base Development System
Primary Software Version: 2015
Primary Software Fixed Version: N/A
Secondary Software: N/A
Does LabVIEW support recursion? How can I create recursive VIs in LabVIEW?
Yes, LabVIEW does support recursion. LabVIEW versions 2009-2015 allows you to use recursive VIs in a very simple process. Refer to the White Paper: Creating Recursive VIs for information about recursion in LabVIEW versions 2009-2015 and how to create a recursive VI. For LabVIEW 8.2 & 8.6, follow the steps listed below to make a VI recursive.
A VI that calls itself is referred to as a recursive VI. In other words, if a LabVIEW application uses recursion, it will open up additional copies of itself in order to complete some desired task. For example, one popular application of recursion is computing a factorial (denoted by the ! symbol). The factorial of any given positive integer is the product of all positive integers from one up to (and including) the given integer. So, 3! equals 3*2*1 = 6. The recursive factorial.vi example attached below computes a result by multiplying an integer by the next smallest integer's factorial (therefore calling itself). In mathematical terms, 3! = 3*(2!). Within this recursive factorial VI, 1! and 0! (a special case) are hard-coded to produce a result of 1. More information is located on the White Paper mentioned above.
It is important to note that a recursive VI must open up additional copies of itself that each occupy their own space in memory. Equivalently, we can call such a VI reentrant. In other words, each instance of the recursive VI has its own data that is distinct from other instances of the VI. The steps listed below ensure that a recursive VI is configured for reentrant behavior.
Steps to Create a Recursive VI
Follow these instructions in order to program a VI that is capable of calling itself:
Open the factorial.vi application attached below for an example of a recursive VI. Running the VI will produce the factorial of the input integer.
KnowledgeBase 2QKDG7DW: Differences Between Reentrant VIs, VI Templates, and Dynamic VIs
White Paper: Creating Recursive VIs
LabVIEW Help: Recursive VIs
LabVIEW Help: Creating a Recursive VI
Report Date: 02/15/2007
Last Updated: 02/02/2016
Document ID: 46EKS80E