[7.9] Passing Optional Parameters To Functions And Programs - Texas Instruments TI-89 Tip List

Graphing calculator
Hide thumbs Also See for TI-89:
Table of Contents

Advertisement

lres is a list of strings to return to the history area. You can use the "@" character to add labels to the
results. Note that the input string lres is stored to the global variable wstr, so that you can also recall
wstr to get the results.
Some examples:
tohome({"45"})
tohome({"45","x=6*po"}) returns
tohome({"@res1","45","@res2","x=6*po"}) returns
cj points out that tohome() does not work if called from a keyboard program; those executed with
[DIAMOND][n], where n is a number from 1 to 9. However, tohome() works if kbdprgmn() is called from
the command line.
(Credit to Timité Hassan and cj for Method 2)

[7.9] Passing optional parameters to functions and programs

Many programming languages support passing optional parameters to functions and programs, but TI
BASIC does not. For example, you might have a function f1() that takes three or four parameters:
f1(p1,p2,p3,p4)
but if the user doesn't supply a value for the third parameter:
f1(p1,p2,,p4)
then the program supplies a default value for p3, or takes some other action. Note that this feature is
supported in some built-in 89/92+ functions, such as LinReg(), for example.
You can simulate this operation to some extent in your own programs by passing the parameters as
lists. For the example above, the call would be
f1(p1,p2,{p3},p4)
The p3 parameter is always included in the call, but it may be an emply list. This can be determined if
dim(p3) = 0. There are other variations on this theme, for example
f1(p1,{p2, p3, p4})
can be used to supply up to three optional parameters. Again dim() is used to determine how many
parameters were supplied.
Alex Astashyn points out that there are some disadvantages to this method:
! You have to write additional code to recognize which arguments are supplied and to provide
defaults.
! You have to remember to enter additional arguments in the list.
! You have to know what the default values are, to decide if you need to change them.
returns
45 : : Stop
45 : x=6*po : : Stop
"res1" : 45 : "res2" : x=6*po : : Stop
7 - 10

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Ti-92+

Table of Contents