Archived: Recursion in LabVIEW 8.2 & 8.6

Publish Date: Feb 21, 2018 | 0 Ratings | 0.00 out of 5 | Print

Overview

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

This article describes the process of making recursive VIs in LabVIEW 8.2 & 8.6.

Table of Contents

  1. Introduction
  2. About Recursion
  3. Reentrant VIs
  4. Steps to Create a Recursive VI
  5. Factorial Example
  6. Additional Resources

1. Introduction

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 Creating Recursive VIs instead.

 

Back to Top

2. 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 hard-coded to produce a result of 1.  More information is located on the White Paper mentioned above.

 

Back to Top

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

 

Back to Top

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

 

Back to Top

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

 

Back to Top

6. Additional Resources

Differences Between Reentrant VIs, VI Templates, and Dynamic VIs
Creating Recursive VIs
LabVIEW Help: Recursive VIs
LabVIEW Help: Creating a Recursive VI

Back to Top

Bookmark & Share


Downloads

Attachments:

factorial.vi


Ratings

Rate this document

Answered Your Question?
Yes No

Submit