# CDC Community

🔹 Historical Conservation 🔹

### Site Tools

tutor:passing_arguments

# Passing Arguments to Subroutines

When you write “show 13.7w,6”, you are passing two pieces of information to the -show- command. You are giving two numerical “arguments” (13.7w and 6) to the TUTOR machinery that performs the -show- operations. Similarly, we created a half-circular arc with “circle radius,0,180” in which we passed three arguments to the TUTOR circle-making machinery. Sometimes certain arguments are optional. For example, “show 13.7w” will use a default second argument of 4 (significant figures), and omitting the last two arguments in a -circle- command (“circle radius”) will cause a full circle to be drawn rather than an arc. When we pass one argument to the -at- command t 1215“), we mean coarse grid; when we pass two arguments (“at 125,375”), we mean fine grid.

This notion of passing arguments to TUTOR commands, with some arguments optional, also applies to your own subroutines, such as unit “halfcirc”. The “halfcirc” subroutine needs three arguments (x, y, and radius) to do its job. We passed these arguments by assigning values to variables and letting “halfcirc” pick up those values and use them:

Notice that the second -do- will use the original “x” and “y”, since these variables have not been changed. It is as though we passed only one argument (“radius”) to the subroutine.

TUTOR permits another way of writing this sequence which looks similar to the way one passes arguments to the “built-in subroutines” (-show-, -circle-, -at-, etc.):

The statement “unit halfcirc(x,y,radius)” tells TUTOR that when this unit is done as a subroutine, arguments are to be passed to it. The statement “do halfeire(150,300,100)- tells TUTOR to pass the listed arguments to the “halfcirc” subroutine for its use. The arguments are passed in the order listed:

do halfcirc(150,300,100) . . (Pass 3 Arguments) . unit halfcirc(x,y,radius)

In this case the variable “y” has not been assigned a new value, so it retains the value it had, which was 300. (The value of “y” could have changed if “halfcirc” itself altered it. For example, if we append ‘`cale y75” to the end of unit “halfcirc”, “y” would now be 75, although it was originally passed the value of 300 by the first -dostatement during the making of the first display.)

Arguments to be passed need not be simple numbers. Each argument can be a complicated expression. The expressions are evaluated, then passed in order:

It is as though we had written:

Just as the -at- command handles its arguments differently depending on the number of arguments (one for coarse grid and two for fine grid), so it is possible for your subroutines to do such things. There is a TUTOR-defined “system variable” named “args” which always contains the number of arguments passed the last time a subroutine was done. By “system variable” we mean a variable separate from the student variables (v1 through v150) whose contents are assigned by TUTOR rather than by you. You do not define system variables; they are already defined for you. (Indeed, if you say “define args =v3”, you will override TUTOR's definition of the meaning of “args”, so that “args” will mean “v3” rather than “the number of arguments passed to a subroutine”.) In Chapter 6 (Conditional Commands) you will see how you could do a variety of things in a subroutine (conditional on the value of “args”) which are similar to the kinds of things the -at- command does.

Our subroutine “halfcirc” uses three student variables: vl, v2, and v3, defined as “x”, “y”, and “radius”. Another subroutine could use the same variables for carrying out its work, but it must be kept in mind that -do-ing this subroutine will affect vl, v2, and v3, since arguments will be passed.

Suppose one subroutine uses another, with “nested” -do-s like this:

. . . do A(5) . ⇩ . ⇩ PASS . ⇩ unit A(v11) \$\$ v11⇐5 do B(3+v11) calc v11⇐10v11 \$\$ v11⇐50 . . . ⇩ Do B(above) value PASS unit B(v25) \$\$ v25⇐8

Variable v11 ends up with the value 50. It is advisable to use different variables in the two subroutines. Here unit A uses v11 and unit B uses v25. It can lead to confusion or even logical errors if B also uses v11 to do its work, since -do-ing B will affect the value of v11 used by A. Here is the structure to be avoided:

. . do A(5) . ⇩ . ⇩ . ⇩ unit A(v11) \$\$ v11⇐5 do B(3+v11) calc v11⇐10v11 \$\$ v11⇐80 . . . ⇩ do B is PASSED to unit B(v11) \$\$ v11⇐8

Now variable v11 ends up with the value 80 rather than 50. This is due to the effect on v11 of the “do B(3+v11)” statement, which assigns the value of 8 to v11 by passing the argument to unit “B”.

This concludes our discussion of calculations for now. We can calculate, save results, use them to make displays, and show the values. In the next section, we will use calculations in association with guiding the sequencing of a lesson. 