Return to Wilcox Associates, Inc. Home Page
View News and Press ReleasesDownload FilesGet Technical SupportAccess the Users AreaContact Wilcox Associates, Inc.View Offsite Links
Created on: September 24, 2002
Last modified: July 16, 2008
Return to Home Page Contact Us

Subroutines Examples

A subroutine is a very useful tool that allows you re-use code without having to write it each time in your program. Subroutines can either be internal or external to a part program. Consider these examples:


Example 1: (Construct a square slot using a subroutine)

This example contains two part programs and uses a subroutine to construct a generic square slot feature. The first part program contains the subroutine calling statement, and the second part program contains the actual subroutine.

Note that the code provided in these examples provides several document (or programmer) comments detailing what's happening in both the calling part program and in the subroutine.

The Calling Part Program

Step 1: First we'll measure four lines that represent the four edges of the slot. Line one will start with one of the long sides, then line two will be a short side, line three will be the second long side, and line four will be the second short side.

Step 2: Next we'll define eight variables, setting each to a value of 0. They will hold the data for the generic square slot that will be constructed.

Step 3: Call the subroutine with the CALLSUB statement:

Things to Note:

  • The calling statement, CALLSUB, at least in our case here, takes four lines and eight variables and passes them to the subroutine "CONSTRUCT_SQUARE_SLOT".
  • The calling statement defines the path of the file where the subroutine resides: "D:\PARTPROGRAMS\MYSUBS.PRG:"
  • It passes all the needed information, into the subroutine as parameters. Note that features must be passed with curly braces {} surrounding the feature ID). Also, note that in order to get information back from a subroutine we must pass it variables within which it can store the data. The slot's XYZ (SLTX, SLTY, SLTZ), the slot's width (SLTW), the slot's length (SLTL), and the slot's IJK (SLTI, SLTJ, SLTK) are all being passed into the subroutine to store the data from the subroutine.

Step 4: Use the returned variables from the subroutine to construct the slot.

So here is our entire part program code:

The Subroutine

In our case the subroutine is an external subroutine, because it resides outside of the calling part program inside a separate part program, called mysubs.prg.

The subroutine is named CONSTRUCT_SQUARE_SLOT. It takes the four line features and the eight variables and calculates the X, Y, Z, (the slot's center), the R (the slot's radius) and the L (the slot's length) of the square slot, as well as the IJK to use, and then it passes all this data back to the original calling part program.

Things to Note:

  • You must always have the line "SUBROUTINE/" containing the subroutine's name and any parameters. You will need a pathway to the part program containing the subroutine if the subroutine is external to the calling program. Passed in parameters are each seperated by a comma.
  • You must also always have the line "ENDSUB/" at the end of the subroutine. Any commands following an ENDSUB statement will be ignored.
  • Every feature, or variable that you pass into a subroutine that you want data back from must also have a unique ID inside the subroutine. They can be the same as the calling program, but that isn't necessary.
  • Everything that is possible inside a regular part program is also valid in a subroutine.

Here is our subroutine code, contained within the mysubs.prg part program.

Back to Top


Example 2:(Subroutine that parses a string).

This subroutine takes a string and parses it at each "Comma" that the user places inside the string. An example of this would be the string: "80,2.0,0.1,0.1". The string has four numbers in it, each separated by a comma. The following subroutine will parse the string and place each number in its own variable that can then be used anyplace a variable can be used. The current displayed version of the subroutine will parse up to four values in the string, but it can be extended to parse as many values as needed.

The Calling Part Program

The call to the subroutine takes the string to be parsed, the number of items to parse out, and one variable ID for each item being parsed out. This particular example requests from the user the desired "Motion parameter" to be used during the execution of the program.

Step 1: Define four variables to be passed into the subroutine: MS for Move Speed. TS for Touch Speed. PH for Prehit Speed and RT for Retract Speed.

Step 2: Prompt the user with a comment for the needed information.

Step 3: Call the subroutine with the CALLSUB command, and send six parameters: the first is the C1.INPUT variable containing the user's string input, second is a number determining how many values are contained in the string input, and the remaining four parameters, are the defined variables which act as holding variables to collect the parsed values.

So, our complete calling code is here:

Step 4: Once the subroutine finishes parsing the values, we give the motion parameters the appropriate variable values.

The Subroutine

The subroutine receives the input string and all the other parameters, assigning them to local variables within the subroutine. It then enters a WHILE loop to parse out each value based on the location of each comma inside the string. It uses the ELEMENT expression to do this.

ELEMENT(<Integer>, <String1>, <String2>)

This expression returns the nth substring (element) from string2 using string1 as the delimiting text that divides the elements in string2.

The loop runs a total of four times and each time it assigns a value to one of the holding variables until all four contain values.

It then displays an Operator comment displaying all of the values.

Here is our complete subroutine code, contained within the mysubs.prg part program:

Back to Top

Back to Hints

 

sales@wilcoxassoc.com | webmaster@wilcoxassoc.com
Copyright © 2002-2012, Wilcox Associates, Inc.
Part of Hexagon Group