User Tools

Site Tools


plato:tutor:showing_variable_values

Showing the Value of a Variable

We have learned how to calculate and how to store results in variables. How do we show these results on the screen? Suppose we perform this calculation:

calc y⇦5sqrt(37) $$ or, y⇦5x371/2 ; "sqrt" means square root

How do we later show the value of y? Assume we have defined y. Perhaps we could use this:

write y

No, that won't work; that will just put the letter β€œy” on the screen. The -write- command is basically a device for displaying non-varying text, not for showing the value contained in a variable. We need another command:

show y

This will show the value of y in an appropriate format (-show- picks an appropriate number of significant figures and will use a scientific format such as 6.7Γ—1013, if the number is large enough to require it). By using -show- instead of -write-, you tell TUTOR that you want the stored value to be shown rather than just the characters in the tag.

The -show- command will normally choose 4 significant figures, so that a typical display might be β€œ-23.47”. You can specify a different value by giving a second β€œargument” (arguments are the individual pieces of the tag of a statement):

show y,8 $$ 8 significant figures

The arguments of the -show- command can, of course, be complicated expressions:

show 10+30cos(2angle),format+2)

In fact, it is a general rule that you can use complicated expressions anywhere in TUTOR statements. For example, ''draw 5rad +225,34L;123-L2,28Lβ€œ!

Here is a short program which uses -show- to display a table (see Figure 4-6) of square roots of the integers from 1 to 15:

define N=v1 unit roots at 310 $$ write titles for the two columns write N at 325 write N1/2 do root,N⇐1,15 * unit root at 410+100N show N at 425+100N show sqrt(N)

The last statement could also be written as β€œshow NW”. 'This technique of making tables, including the use of the -do- index (N) to position the displays (as in β€œat 425Β±100N”) is an important and powerful tool.

There are other commands for displaying variables: -showe- (exponential), -showt- (tabular), -showa- (alphanumeric), -showo- (octal), and -showz- (show trailing zeroes). These are described in detail in the reference material mentioned in Appendix A.

Although -write- is basically designed for non-variable text, combinations of text and variables occur so often that TUTOR makes it easy to β€œembed” a -show- command within a -write-:

write The area was βŠ€s,13.7w,6⊁ square miles.

The embedded β€œs” indicates a -show- command and the remainder β€œ13.7w,6” is its tag. Other permissible abbreviations include β€œo” (showo), β€œa” (showa), β€œe” (showe), β€œt” (showt) and β€œz” (showz). The above -write- statement is equivalent to:

write The area was show 13.7w,6 write square miles.

Passing Arguments to Subroutines

plato/tutor/showing_variable_values.txt Β· Last modified: 2023/08/05 18:55 by Site Administrator