Previous Up Next

3.4.1  Defining functions

Similar to how you can assign a value to a variable (see Section 3.3.2), 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:

f(3)

you will get:

     
9           

You can define an anonymous function, namely a function without a name, with the -> operator; for example, the squaring function can be defined by

x->x^2

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 you could enter

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

or

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

to define a function which takes the lengths of the two legs of a right triangle and returns the hypotenuse.


Previous Up Next