Creating User-defined Functions and Programmes
You can use the Define command to create your own functions and programmes. You can create them in the Calculator application or in the Programme Editor and then use them in other TI-Nspire™ applications.
For more information, see Overview of the Programme Editor and Libraries.
Suppose you want to define a function named cube() that calculates the cube of a number or variable.
1. | On the Calculator entry line, type Define cube(x)=x^3 and press Enter. |
The message “Done” confirms that the function has been defined.
2. | Type cube(2) and press Enter to test the function. |
You can define a function consisting of multiple statements entered on separate lines. A multiple-line function may be easier to read than one with multiple statements separated by colons.
Note: You can create multiple-line functions only by using the Define command. You cannot use the := or → operators to create multiple-line definitions. The Func...EndFunc template serves as a container for the statements.
As an example, define a function named g(x,y) that compares two arguments x and y. If argument x > argument y, the function should return the value of x. Otherwise, it should return the value of y.
1. | On the Calculator entry line, type Define g(x,y)=. Do not press Enter yet. |
2. | Insert the Func...EndFunc template. |
From the Functions & Programmes menu, select Func...EndFunc.
Calculator inserts the template.
3. | Insert the If...Then...Else...EndIf template. |
From the Functions & Programmes menu, select Control, and then select If...Then...Else...EndIf.
Calculator inserts the template.
4. | Type the remaining parts of the function, using the arrow keys to move the cursor from line to line. |
5. | Press Enter to complete the definition. |
6. | Evaluate g(3,-7) to test the function. |
Within a multi-line template such as Func...EndFunc or If...EndIf, you can start a new line without completing the definition.
• | Handheld: Press @ instead of ·. |
• | Windows®: Hold Alt and press Enter. |
• | Macintosh®: Hold Option and press Enter. |
As an example, define a function sumIntegers(x) that calculates the cumulative sum of integers from 1 to x.
1. | On the Calculator entry line, type Define sumIntegers(x)=. Do not press Enter yet. |
2. | Insert the Func...EndFunc template. |
From the Functions & Programmes menu, select Func...EndFunc.
Calculator inserts the template.
3. | Type the following lines, pressing @ or Alt+Enter at the end of each line. |
4. | After typing Return tmpsum, press Enter to complete the definition. |
5. | Evaluate sumIntegers(5) to test the function. |
Defining a programme is similar to defining a multiple-line function. The Prgm...EndPrgm template serves as a container for the programme statements.
As an example, create a programme named g(x,y) that compares two arguments. Based on the comparison, the programme should show the text “x>y” or “x≤y” (showing the values of x and y in the text).
1. | On the Calculator entry line, type Define prog1(x,y)=. Do not press Enter yet. |
2. | Insert the Prgm...EndPrgm template. |
From the Functions & Programmes menu, select Prgm...EndPrgm.
3. | Insert the If...Then...Else...EndIf template. |
From the Functions & Programmes menu, select Control, and then select If...Then...Else...EndIf.
4. | Type the remaining parts of the function, using the arrow keys to move the cursor from line to line. Use the Symbol Palette to select the "≤“ symbol. |
5. | Press Enter to complete the definition. |
6. | Execute prog1(3,-7)to test the programme. |
You might want to reuse or modify a function or programme that you have defined.
1. | Show the list of defined functions. |
From the Actions menu, select Recall Definition.
2. | Select the name from the list. |
The definition (for example, Define f(x)=1/x+3) is pasted into the entry line for editing.