Summary: The creation of this content was supported in some part by NSF grant 0538934.
G is a high level, data-flow graphical programming language designed to develop applications that are
The program is a block diagram edited in the Block Diagram programming window.
![]() |
The program input data and results are manipulated and displayed in the Front Panel window.
![]() |
The first program is to display the text “Hello graphical interactive parallel multicore world” in the Front Panel window.
Right click on the Block Diagram window and select String Constant from the Functions >> Programming >> String menu.
Drag and drop the String Constant onto the Block Diagram window as show in Figure 3.
![]() |
Type in “Hello graphical interactive parallel multicore world.” in the String Constant.
Right click in the Front Panel window and select a String Indicator from the Controls >> Modern >> String & Path menu.
![]() |
Drop it into the Front Panel window.
![]() |
Return to the programming window. Notice the string terminal corresponding to the string indicator in the Front Panel window. As you approach the string constant from the right, the wiring terminal is highlighted and the pointer turns to wire spooler.
![]() |
Click the “Hello graphical interactive parallel multicore world” terminal and then click on the String Indicator triangular terminal to wire the terminals.
![]() |
Save your program as Hello, World.vi. Return to the Front Panel window. Click the run button (➯). You have successfully completed and executed your first G program.
![]() |
The next program converts degrees from Fahrenheit to Celsius using the formula
In the Block Diagram window, select the subtract, multiply and divide from the Functions >> Mathematics >> Numeric menu
![]() |
Wire the subtract, multiply and divide functions as shown in Figure 11.
Right click on the upper left terminal of the subtract function and select Create >> Control from the pop-up menu.
![]() |
Re-label x as Fahrenheit and wire the terminal as shown in Figure 13.
![]() |
Right click on the lower left terminal of the subtract function and select Create >> Constant and type 32.0.
![]() |
Repeat the process to generate numeric constants for the multiply and divide function with 5.0 and 9.0 respectively.
![]() |
To complete the program, right click on the right terminal of the divide function and select Create >> Indicator. Re-label x/y as Celsius. The final diagram is shown in Figure 16.
![]() |
Switch to the Front Panel window to run the program. Save the program as Celsius.vi. Try various Fahrenheit values to see the corresponding Celsius values. You have successfully finished a Fahrenheit to Celsius calculator.
Click on empty space and drag to select the entire diagram.
![]() |
The selected diagram is highlighted as shown in Figure 19
![]() |
From the Edit menu select Create SubVI to create a G function. The resulting diagram is shown in Figure 20.
![]() |
From the File menu select Save All and save the Untitled function as Fahrenheit to Celsius.vi.
![]() |
Open the Fahrenheit to Celsius.vi by double clicking on the icon. Right click on the icon editor (upper right corner) and select Edit Icon…
![]() |
This pops-up the Icon Editor. Edit the function’s icon.
![]() |
After editing the icon, the function’s icon is shown in the upper right corner of the Front Panel window. Save the function, plug in various input values and run the function. Save the function.
![]() |
Close the Fahrenheit to Celsius function and return to the Celsius Block Diagram windows. The Celsius diagram reflects the updated Fahrenheit to Celsius icon
![]() |
This program determines if a year is a leap year or not. A leap year is divisible by 4 but not by 100, except when it is divisible by 400. A number x is divisible by a number y if the remainder of x/y is identical to zero, i.e. Rem(x/y)=0 is true therefore
where And, Or and Not are Boolean operators.
For example:
1900 is not a leap year because it is divisible by 100
1970 is not a leap year because it is not divisible by 4
1980 is a leap year because it is divisible by 4 but not by 100
2000 is a leap year because it is divisible by 400
Start a new G program and right click on the Block Diagram window. Go to the Functions >> Programming >> Numeric menu in the Block Diagram window.
![]() |
Select three copies of the Quotient & Remainder function and three numeric constants. Type in 4, 100 and 400 for the numeric constants and wire these constants to the lower input terminal (corresponding to the dividend) of the Quotient & Remainder function.
![]() |
From the Functions >> Programming >> Comparison menu, select 2 copies of the Equal to Zero function and one copy of the Not Equal to Zero function.
![]() |
Organize the comparison operations as show in the diagram.
![]() |
From the Functions >> Programming >> Boolean menu select the AND and OR operators
![]() |
Place the Boolean operators as shown in Figure 31.
![]() |
From the Functions >> Programming >> Structures menu, click on the Case Structure.
![]() |
Click and drag on the Block Diagram window to create the Case Structure.
![]() |
The True diagram option is indicated at the top of the case structure.
![]() |
Drop a string constant and type “Is a Leap Year”.
![]() |
Click on the down arrowhead next to the True label and select the False option.
![]() |
Drop another string constant and type “Is not a Leap Year”.
![]() |
Go to the Front Panel window and place a numeric input and an output string. Re-label the numeric input to Year and the output string to Message.
![]() |
Right click on Year and select Representation >> I32 from the numeric pop-up menu.
![]() |
Arrange the Year and Message terminals in the Block Diagram window as shown in the figure.
![]() |
Wire the OR operator is to the “?” in the case structure and the string constant “Is not a Leap Year” is wired to Message.
![]() |
Select the True option and Wire the “Is a Leap Year” string constant to the output terminal of the Case Structure.
![]() |
Save the program as Leap Year.vi, enter Year values and run the program to determine whether the value of Year is that of a leap year or not.
![]() |
Right click on the Front Panel window and select Array from the Controls >> Modern >> Arrays, Matrix & Cluster menu, and drop an array onto the Front Panel window. The array structure consists of an index or element offset (left portion of the structure) and the array elements (right portion of the structure). When the array structure is placed on the Front Panel window, the data type of the array is undefined as indicated by the grayed out portion of the array.
![]() |
To define the array data type, drag and drop a data type onto the array structure. For instance, to create an input array of numbers, place Numeric Control into the array structure.
![]() |
At this point, the numeric array is an Empty or Null array because no elements of the array have been defined. This is indicated by the grayed out numeric control within the array structure.
![]() |
Define elements of an input array by selecting the offset and entering its value. For instance, at offset = 4, enter the value 0.0. This defines Numeric Input Array as {0, 0, 0, 0, 0}.
![]() |
An output array is created similarly to an input array with the exception that an output data type needs to be dropped into the array structure.
![]() |
This program converts an array of Fahrenheit values to Celsius. Create numeric input and output arrays and label them Fahrenheit and Celsius respectively. In the Fahrenheit array enter the values 0, 20, 40, 60, 80, 100, 120, 140, 160, 180 and 200 at offsets 0 through 10 as shown in Figure 49.
![]() |
Right click in the Block Diagram window, navigate to Programming >> Structures and click on For Loop.
![]() |
Click and drag to create the For Loop as shown in Figure 51 and Figure 52.
![]() |
![]() |
Right click inside the For Loop and select Select a VI… from the pop-up menu. Find the Fahrenheit to Celsius.vi and click OK. Drop the function inside the For Loop.
![]() |
To complete the program, wire the Fahrenheit input array to the input terminal of the Fahrenheit to Celsius function and wire the output terminal of the Fahrenheit to Celsius function to the Celsius output array.
![]() |
This program uses the For Loop to select each element in the Fahrenheit input array, converts that value to Celsius and saves the results in the Celsius output array. Save the program as Fahrenheit to Celsius For Loop.vi and run the program.
![]() |
The Celsius output array contains: Celsius = {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333}
The next program will generate Fahrenheit values and convert them to Celsius until a condition is met to stop the iterations in a While Loop. In the Block Diagram window, select the While Loop structure by clicking on it from the Functions >> Programming >> Structures menu.
Click and drag to create the While Loop structure.
![]() |
![]() |
![]() |
In the Front Panel window, create two numeric output arrays. Label them Fahrenheit and Celsius.
![]() |
Re-arrange the diagram as in Figure 60.
![]() |
From the Functions menu, select Multiply function and a couple of numeric constants. Type in 20.0 and 300.0 for the numeric constants. Select the Fahrenheit to Celsius.vi and drop it inside the While Loop. Re-arrange the diagram to look like Figure 61.
![]() |
From the Functions >> Programming >> Comparison menu select the Greater or Equal operator.
![]() |
Wire the While Loop components as shown in Figure 63.
![]() |
Wire the output of the Multiply operation to the Fahrenheit and the output of the Fahrenheit to Celsius function to the Celsius numeric output arrays. The connections between the While Loop and the Fahrenheit and Celsius arrays are broken (see Figure 64).
![]() |
To repair the broken connections, roll over the mouse pointer to the Loop Tunnel.
![]() |
Right click on the Loop Tunnel and select Enable Indexing from the pop-up menu.
![]() |
This enables values to accumulate and store the results into an array.
Repeat for the Celsius array.
![]() |
Each iteration of the While Loop in this program generates an i × 20 Fahrenheit value and converts it to Celsius. The While Loop stops iterating when the generated Fahrenheit value is greater than or equal to 300. The resulting arrays are stored in the Fahrenheit and Celsius numeric output arrays.
Save the program as Fahrenheit to Celsius While Loop.vi and run it. The program generates the following results:
![]() |
Fahrenheit = {0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 240, 260, 280, 300}
Celsius = {-17.7778, -6.6667, 4.44444, 15.5556, 26.6667, 37.7778, 48.8889, 60, 71.1111, 82.2222, 93.3333, 104.444, 115.556, 126.667, 137.778, 148.889}
![]() |
Using the previous G program example, we will now visualize the results by adding a graph to the Front Panel windows. Right click on the Front Panel window. Select XY Graph from the Controls >> Modern >> Graph menu.
![]() |
Drop the XY Graph in the Front Panel window. Double click on the x and y axis labels and rename Time to Fahrenheit and Amplitude to Celsius.
![]() |
The Block Diagram window contains the XY Graph terminal.
![]() |
Select Bundle from the Functions >> Programming >> Cluster, Class & Variant menu
![]() |
Drop it on the diagram as shown in Figure 74.
![]() |
Wire the Fahrenheit and Celsius results to the input Bundle terminals and the output Bundle terminal to the XY Graph.
Save the program and run it. The resulting graph is shown in the figure below.
![]() |
![]() |
This G program shows how G allows programmers to develop interactive programs. Create the following G program and wire it as shown in the figure below.
![]() |
In the Front Panel window, from the Functions >> Modern >> Numeric select the vertical pointer slide. From the Functions >> Modern >> Graph select Waveform Chart.
|
Re-label the vertical pointer slide as Amplitude and the waveform chart as Sine Wave. Re-arrange to GUI to look like the figure below.
![]() |
Right click on Sine Wave and select Properties from the pop-up menu.
![]() |
Select the Scales tab and change Maximum to 1023. Sine Wave will display 1024 samples.
![]() |
Click on the down arrow located to the right of Time (X-Axis) and select Amplitude (Y-Axis).
![]() |
De-select Autoscale and change the Minimum and Maximum values to -10 and 10. Click OK.
![]() |
In the Block Diagram window, re-arrange the Amplitude and Sine Wave terminals and finish the program as shown in Figure 84.
![]() |
Scroll the mouse pointer over the Loop Control…
![]() |
And right click on the Loop Control and from the pop-up menu select Create Control.
![]() |
A stop terminal is created…
![]() |
With the corresponding stop Boolean input control. Save the G program as Interactivity.vi.
![]() |
Run the G program.
![]() |
While the program is running, change the Amplitude and watch the graph update to reflect the interactive changes.
![]() |
To end the G program, simply click on the stop button.
Congratulations. You have successfully completed and executed your first interactive G program.
![]() |
Save a copy of Interactivity.vi as Parallel Programming.vi. Select the while loop as shown in Figure 92.
![]() |
From the menu select Edit >> Copy.
![]() |
Create a copy of the while loop and its contents by selecting Edit >> Paste. Organize the diagram as shown in the figure below.
![]() |
Go the Front Panel window and organize the input and output controls as shown in the figure below.
![]() |
Congratulations!!! You have just completed your first parallel interactive program using G. Save the program, run it and interact with it. To end this program click on stop and stop 2.
![]() |
Save a copy of Parallel Programming.vi as Multicore Programming.vi. If you have a multicore computer, CONGRATULATIONS!!! You have just completed your first multicore G program.
![]() |
This program shows the polymorphic properties of G. Create the G program shown below.
Notice that the Subtract and Multiply operations allow arrays to be wired in the G program.
![]() |