#Functions

# Although Maple has a large library of standard functions, we often need
# to define new ones.  For example, to define a fourth order polynomial:
#
#           p(x) = 18 x^4  + 69 x^3  - 40 x^2  - 124 x - 48
# we enter the Maple code

 p := x -> 18*x^4 + 69*x^3 - 40*x^2 - 124*x - 48;

# Think of the symbol -> as an arrow: it tells what to do with the input
# x, namely, produce the output 18*x^4 + 69*x^3 - 40*x^2 -124*x - 48. To
# understand the historical origins of this notation, read about lambda
# functions, a subject of interest to computer science areas. Once the
# function p is defined, we can do the usual computations with it, e.g.,

 p(-2);
 p( 1/2 );
 p( a+b );
 simplify(%);

# It is important to keep in mind that functions and expressions are
# different kinds of mathematical objects.  Mathematicians know this, and
# so does Maple.  Compare the results of the following:

 p;    # function
 p(x); # expression
 p(y); # expression
 p(3); # expression
 
# As further proof, try the following:

 factor(p);
 factor( p(x) );
 plot( p, -2..2 );
 plot( p(x), x = -2..2 ); # same result

# Functions of several variables can be defined as easily as can
# functions of a single variable:

 f := (x,y) -> exp(-x) * sin(y);
 f(1,2);
 g := (x,y) -> alpha*exp(-k*x)*sin(w*y);
 g(1,2);
 alpha := 2; k := 3; g(1,2);

 w := 3.5; g(1,2);
 alpha := 'alpha'; g(1,2);