Similar to how you can assign a value to a variable (see Section 5.4.2), you can use the := and => operators to define a function; both
and
give the name f to the function which takes a value and
returns the square of the value. In either case, if you then enter:
Input:
you will get:
Output:
9 |
You can define an anonymous function, namely a function without a name, with the -> operator; the squaring function can be written
You can use this form of the function to assign it a name; both
and
are alternate ways to define f as the squaring function.
You can similarly define functions of more than one variable. For example, to define a function which takes the lengths of the two legs of a right triangle and returns the hypotenuse, you could enter
or