User Functions for Unit Conversion
If you perform particular unit conversions often, you can write user
functions for those conversions. In this section you'll write user func-
tions O-C and C-O that convert between ounces and grams; since
they're user functions, you can use them in either RPN or algebraic
Recall that user functions must fulfill two requirements:
• They must explicitly indicate their arguments .
• They must return exactly one result.
First write O-C.
Begin the program and indicate the argument.
The right arrow indicates that the following name is a
which will exist only within this program.
Define the conversion.
x ['] oz ['] [']
The closing delimiters are added for you.
This program means: take an argument from the stack (in RPN syn-
tax) or from the expression (in algebraic syntax) and call it
x; convert x
from ounces to grams; and drop the gram unit from the stack.
Store the program in a variable O-C.
14: Unit Conversion