Re: function definition

• To: mathgroup at christensen.Cybernetics.NET
• Subject: Re: function definition
• From: Leendert van Gastel <gastel at can.nl>
• Date: Fri, 21 Oct 1994 17:42:53 +0100

```> Can anyone explain the principle for function definition that yields the
> following:
> First define f2[x]:
> in: f2[x_] := x + 2;
>
> in: ?f2
> out: Global`f2
> f2[x_] := x + 2
>
> Now re-define f2[x], and we see that the old definition has been overwritten:
> in: f2[x_] := x + 3;
>
> in: ?f2
> out: Global`f2
> f2[x_] := x + 3
>
> But now define f2[y], and see that the old definition
> is the one that is used by Mma!
> in: f2[y_] := y + 4;
> out: ?f2
> Global`f2
> f2[x_] := x + 3
> f2[y_] := y + 4
>
> Check out values of f2. They're all from the x + 3 definition:
> in: f2[x]
> out: 3 + x
> in: f2[y]
> out: 3 + y
> in: f2[1]
> out: 4
>

Dear David,

In the last case, Mathematica does not overwrite the previous
definition. It only overwrites when the left-hand-side of a
new definition is exactly of the form of the previous definition.
Personally, I think it should overwrite in the case
f2[x_] := ... and f2[y_] := ... , but is does not.

Given this fact, the behaviour is predictable. Mathematica always
writes a new rule below a previous rule (if it does not recognize
the pattern as being more special, as in f2[x_] := ... versus f2[3] = ...)
and Mathematica always takes for evaluation the rule that has highest priority.
The priority you can check with "? f2", the higher in the list, the higher
the priority. So there is no other way to get at
the definition f2[y_] := y + 4, then to delete the f2[x_] - definition,
for instance by

In[1]:= f2[x_] := x + 3

In[2]:= f2[y_] := y + 4

In[3]:= ? f2
Global`f2

f2[x_] := x + 3

f2[y_] := y + 4

In[4]:= f2[x_] =.

In[5]:= ? f2
Global`f2

f2[y_] := y + 4

In[5]:= f2[1]

Out[5]= 5

Leendert van Gastel
CAN Expertise Centre
Kruislaan 419
1098 VA Amsterdam

email: gastel at can.nl
tel: +31 - (0)20 - 5608420
fax: +31 - (0)20 - 5608448

```

• Prev by Date: surface generation using an irregular set of points
• Next by Date: Mathematica Programming
• Previous by thread: RE: function definition
• Next by thread: Re: function definition