Re: Function as an argument
- To: mathgroup at smc.vnet.net
- Subject: [mg76714] Re: Function as an argument
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 26 May 2007 04:33:24 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <f36ddj$71m$1@smc.vnet.net>
dXdYdZdu wrote: > Hi, I have problem. I want to do something like this: > > ss[x_] = 2 + x^3 ; > MyFunction[expression_] := Module[{}, Return[expression[2]]] > Calling MyFunction[ss] will produce 10 and this is working case. > > but this is what I can not do: > > MyFunction[2 + x^3] > > Is there a way to make this to work as well? The following code will do it. Note that Module is not needed since you do not define any local variable. Also you do not have to end your definition with Return because Mathematica returns the value of the last expression. In[1]:= ss[x_] = 2 + x^3; myFunction[expr_Symbol] := expr[2] myFunction[expr_] := expr /. x -> 2 myFunction[ss] myFunction[ss[x]] myFunction[2 + x^3] Out[4]= 10 Out[5]= 10 Out[6]= 10 Regards, Jean-Marc