MathGroup Archive 2002

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

Search the Archive

RE: passing functions as parameters


There is no problem in passing functions to a routine. It can be done
directly.

f1[x_] := x^3 - x + 1
f2[x_] := -x

g[x_, y_, f_] := If[f[x] > f[y], "Done", "Waiting"]

g[2, 3, f1]
Waiting

g[2, 3, f2]
Done

g[2, 3, Sin]
Done

Or you can simply pass a pure function without writing a definition in
advanced.

g[2, 3, #^2 + 2Sin[#] &]
Waiting

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

> From: GoranG [mailto:icmc2MAKNUTIOVO at pop.tel.hr]
To: mathgroup at smc.vnet.net
>
>
> How would one pass one function to another?
>
> I need to be able to define functions in the following fashion:
>
> f[x_]:=x^3-x+1
>
> g[x_,y_,somefunction?]:=If[somefunction[x]>somefunction[y],"Done",
> "Waiting")
>
> to be able to later on evaluate
>
> g[2.,3., f?]
>
> I have tried couple of paths with no success and would really like to
> avoid using global function in function g.
>
> All the best...
>
>



  • Prev by Date: RE: passing functions as parameters
  • Next by Date: Re: a complex function appears in a real integration
  • Previous by thread: RE: passing functions as parameters
  • Next by thread: RE: passing functions as parameters