Academic Company Events Community Support Solutions Products & Services Contact NI MyNI
26 ratings:
 3.57 out of 5   

Archived: Recursion in LabVIEW 8.2 & 8.6

This document has been archived and is no longer updated by National Instruments

Primary Software: LabVIEW Development Systems>>LabVIEW Base Development System
Primary Software Version: 2017
Primary Software Fixed Version: N/A
Secondary Software: N/A

How can I create recursive VIs in LabVIEW 8.2 & 8.6?

To make a VI recursive in LabVIEW 8.2 and 8.6, follow the steps below. If you're running LabVIEW 2009 or later, refer to the White Paper: Creating Recursive VIs instead.

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 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.

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 in LabVIEW 8.2 & 8.6:

  1. Place the desired controls and indicators on the front panel (be sure they have the appropriate representation), then configure the connector pane by right clicking on the icon at the top right of the front panel and selecting Show Connector Pane just as you would for creating a sub-VI.

  2. Make the VI reentrant by selecting File»VI Properties»Execution»Reentrant execution.

  3. Within the VI block diagram, a copy of the current VI must be called at runtime using the Call By Reference Node.  First, open the functions palette and select Application Control»Open VI Reference.  Wire in the recursive VI's path, and set the options input to a constant 8.  This enables calling a reentrant VI by reference.

  4. Right click on the type specifier VI refnum terminal of the Open VI Reference icon, and click Create»Constant.  Right click on the created constant and choose Select VI Server Class»Browse.  Navigate to the recursive VI's location and select OK. Notice that the connector pane of the type specifier constant now represents the recursive VI's connector pane.

  5. Connect the newly opened VI reference to a Call By Reference Node located on the functions palette under Application Control»Call By Reference Node.  Also connect wires to the input and output terminals of the VI being called by reference (dictated by the application's functionality).

  6. Close the VI reference using the Close VI Reference icon located on the functions palette under Application Control»Close VI Reference.

  7. Ensure that your recursive VI has an exit condition.  In other words, some case must exist that returns a value without calling an additional copy of the recursive VI.  This is the final step; the VI is now configured properly for recursion.

Factorial Example
Open the 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
White Paper: Creating Recursive VIs
LabVIEW Help: Recursive VIs
LabVIEW Help: Creating a Recursive VI


Report Date: 02/15/2007
Last Updated: 09/12/2017
Document ID: 46EKS80E

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
  1 2 3 4 5
Please Contact NI for all product and support inquiries.submit