       Re: Fuction definition

• To: mathgroup at smc.vnet.net
• Subject: [mg73686] Re: Fuction definition
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sat, 24 Feb 2007 02:26:20 -0500 (EST)
• Organization: The Open University, Milton Keynes, UK
• References: <erme23\$i63\$1@smc.vnet.net>

```bar at ANTYSPAM.ap.krakow.pl wrote:
> Hi
>
> When I try :
> ------------------
> p = 1;
> f1[x_] := p Sin[Pi x];
> p = 2;
> f2[x_] := p Sin[Pi x];
>
> f1[0.5]
> f2[0.5]
>
> Out=2.
>
> Out=2.
> -----------------
> When I use = instead of := it works OK.
> Why ?

SetDelayed, :=, evaluates the body of the definition only when the the
function is called. Therefore, f1 looks at the value of p only when it
is called by f1[0.5]. At that time, the current value of p is 2, not 1.

In:=
p = 1;
f1[x_] := p*Sin[Pi*x];
f1[0.5]

Out=
1.

In:=
p = 2;
f2[x_] := p*Sin[Pi*x];
f1[0.5]
f2[0.5]

Out=
2.

Out=
2.

> It is safely to use "=" in function definition ?

The usual way for defining a function is to use SetDelayed, not Set.

> I have to integrate and differentiate such function in the future.
> In my case function depends on about 20 parameters, which are calculated
> three times, (I want to obtain three functions)

You could define your functions in the following way:

In:=
f3[p_][x_] := p*Sin[Pi*x];
f3[0.5]

Out=
4.

> I'm not sure is this good idea to use '=' ?

Short answer: no, not for defining a function.

> Regards , Olaf

Regards,
Jean-Marc

```

• Prev by Date: Re: Fw: 2
• Next by Date: Re: Fuction definition
• Previous by thread: Re: Fw: Re: Fw: 2
• Next by thread: Re: Fuction definition