Previous Up Next

5.5.1  Defining functions

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

f(x):= x^2

and

x^2 => f(x)

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:

f(3)

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

x -> x^2

You can use this form of the function to assign it a name; both

f:= x -> x^2

and

x -> x^2 => f

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

hypot(a,b):= sqrt(a^2 + b^2)

or

hypot:= (a,b) -> sqrt(a^2 + b^2)

Previous Up Next