Previous Up Next

4.7.1  Defining functions

You can use the := and => operators to define functions; 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. If you then enter

f(3)

you will get

9

You can give Xcas a function without a name with the -> operator; the squaring function can be written without a name as

x -> x^2

You can use this form of the function to assign it to 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