Re: variable definition help
- To: mathgroup at smc.vnet.net
- Subject: [mg42392] Re: variable definition help
- From: Mark Westwood <markw at epcc.ed.ac.uk>
- Date: Fri, 4 Jul 2003 01:33:10 -0400 (EDT)
- Organization: Edinburgh Parallel Computing Centre
- References: <be101c$2jl$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Jay, f[x] and f[x_] are not really different approaches, they mean quite different things. Take a simple function to square its input: f1[x] := x*x defines f[x] to be x*x, while f2[x_] := x*x defines a function which will return the square of its argument whenever . The difference is readily apparent if you (a) define the functions f1 and f2 and then try f1[y] and f2[y]. The first will return unevaluated, the second will return y*y. The key difference is that x is a symbol and x_ is a pattern - take a good look at the Mathematica Book (available in the Help Browser if you don't have a printed copy to hand) under (a) patterns and (b) defining your own functions. As for f on its own, that's just another symbol and without considerable extra work won't turn into a function. Hope some of this helps Regards Mark Westwood seferiad wrote: > When defining functions, any of the following seem to be commonly used > (assuming the function is of one variable, x): > > f or f[x] or f[x_]. > > Can someone point me to an explanation that defines the pros/cons of using > one approach over the other. In particular, when I should use f[x] vs. > f[x_]. > > Thanks, Jay >