MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg107803] Re: Functions
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Fri, 26 Feb 2010 04:08:14 -0500 (EST)

On 2/25/10 at 7:45 AM, matteo.diplomacy at gmail.com (Allamarein) wrote:

>I want to define f[x_]=a x^2 and after g[a_,f_]= 2a+f so g is an "a"
>"x" function and where "f" is the previous function. Giving to g a
>value for "a" and "x", I'd get a correct "g" result. Which is it the
>correct listed?

There are several ways to achieve what you want. The smallest
change to your code needed to obtain the desired result would be
to define the functions as:

f[x_]=a x^2;
g[a_,x_}= 2 a + f[x];

But, usually (not always) it is better to use SetDelayed (:=)
rather than Set (=) so that evaluate occurs when the function is
used rather than when it is defined. That would be done as follows:

f[x_]:= a x^2
g[a_, x_]= 2 a + f[x];

Note, I've used Set not SetDelayed in the definition of g. Since
f is defined in terms of a single argument x, it is necessary to
evaluate f[x] when defining g. Otherwise, f will not see the
value of a provided to g.

While the above works, I would do the following:

f[x_, a_]:= a x^2
g[x_, a_]:= 2 a f[x, a]

This causes evaluation to occur when g is evaluated and makes
both f and g depend on arguments passed to them. Making
functions depend on things not directly passed to them often
causes unexpected results when the function is used elsewhere in
a notebook. And this kind of problem can be difficult to find.



  • Prev by Date: Re: square roots in Q[r]
  • Next by Date: Re: Seeking your advice: ListLinePlot with null values
  • Previous by thread: Re: Functions
  • Next by thread: minimize's constraint