Often, we want a program to perform the same steps, or just about the same steps, at different places in the program. The simple solution is to copy and paste the desired steps from one place to another in the program. Unfortunately, this can enlarge our program, making it harder to work with, and increase likelihood of errors. An alternative is to create a subchart that performs the desired steps, then call the subchart at the places in our program where those steps must be performed.
Subcharts created by the programmer are similar to procedures already
available in Raptor. Consider
the Draw_Circle graphics procedure. If
the programmer had to write all the steps for drawing a circle (that is,
finding the coordinates of all pixels
which must be set to the desired color and updating them) instead of calling
this procedure, a program would require dozens of symbols to draw a single
circle. If
a second circle had to be drawn, dozens more symbols would be required.
Fortunately,
the call to Draw_Circle hides
the details of drawing a circle from our program. Subcharts
written by the Raptor programmer can do the same. They
are even used in the same was as built-in Raptor procedures
Subcharts can break the Raptor program into logical parts which are called as needed by the main Raptor program. This simplifies design, ensures that flowcharts don't grow to unwieldy sizes, and reduces the chance for errors.
Variables are shared throughout the main flowchart and all subcharts. A variable given a value in one subchart or in the main flowchart will have that value (unless it's subsequently changed) in all other subcharts. Warning! The programmer must be very careful not to inadvertently overwrite shared values by temporarily using a variable in a manner inconsistent with its global program usage. For example, assume that, in the main flowchart, a variable called max is assigned the number of items to process. Assume further that a subchart calculates the maximum value in an array and stores that value in a variable called max. The original meaning and value of max as defined in the main program is now lost!
For more on passing data to and from subcharts, including a suggestion on how to avoid naming conflicts, click here.