Advice: Defining functions

Here is a correct way of defining a function:

> f:= x -> x^2 + 1;

f := proc (x) options operator, arrow; x^2+1 end pr...

You can then evaluate such a function with a constant or variable argument:

> f(u);

u^2+1

> f(3);

10

Similarly, for a function of several variables (note the parentheses):

> g:= (x,y) -> x+y^2;

g := proc (x, y) options operator, arrow; x+y^2 end...

> g(u,v);

u+v^2

On the other hand, the following defines an expression rather than a function:

> h:= x^2 + 1;

h := x^2+1

This is no problem if that's what you wanted, but functions and expressions are used in different ways. In particular, you get a strange-looking result if you try to evaluate h as you would a function:

> h(u);

x(u)^2+1

In order to evaluate h for some value of x , you must use subs or eval .

> subs(x=u, h);

u^2+1

> eval(h,x=u);

u^2+1

Since in ordinary mathematics we often speak of "the function f(x) = x^2+1 ", a common beginner's mistake is to define f(x) instead of defining f .

> F(x):= x^2 + 1;

F(x) := x^2+1

Beware! Everything seems fine, as long as you use only F(x) . But when you try to use F with some other argument, it appears to be undefined:

> F(u);

F(u)

This is not surprising, since in fact it is only F(x) , and not F of anything else, that you have defined. There is a legitimate use of such a construction (it places a value in the remember table of F ).

See also: function , remember , subs

Maple Advisor Database R. Israel, 1997