Recursion in LabVIEWPrimary Software: Primary Software Version: 2009 Primary Software Fixed Version: N/A Secondary Software: N/A
Problem: Does LabVIEW support recursion? How can I create recursive VIs in LabVIEW? Solution: Yes, LabVIEW does support recursion. LabVIEW versions 20092011 allows you to use recursive VIs in a very simple process. Refer to the Developer Zone Tutorial: Creating Recursive VIs for information about recursion in LabVIEW versions 20092011 and how to create a recursive VI. For LabVIEW 8.2 & 8.6, follow the steps listed below to make a VI recursive. About Recursion 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 hardcoded to produce a result of 1. More information is located on the NI Developer Zone mentioned above. Reentrant VIs 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:
Factorial Example 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. Related Links: KnowledgeBase 2QKDG7DW: Differences Between Reentrant VIs, VI Templates, and Dynamic VIs Developer Zone Tutorial: Creating Recursive VIs LabVIEW Help: Recursive VIs LabVIEW Help: Creating a Recursive VI Attachments:
Report Date: 02/15/2007 Last Updated: 10/28/2011 Document ID: 46EKS80E
