I've always thought
(2#+1)&
and
Function[x,2x+1]
are interchangeable in common sense. But,
Clear[F]; F[x_] := (2 # + 1) &[x^2]; F[3]
(* 19 *)
Clear[F]; F[a_] := Function[x, 2 x + 1][a^2]; F[3]
(* 19 *)
Clear[F]; F[x_] := Function[x, 2 x + 1][x^2]; F[3]
(* Function::flpar: Parameter specification 3 in Function[3,2 3+1] should be a symbol or a list of symbols. *)
(* Function::flpar: Parameter specification 3 in Function[3,2 3+1] should be a symbol or a list of symbols. *)
(* Function[3, 2 3 + 1][9] *)
As you see, it seems mathematica provokes an error if you use same variable used in Function[ ], to define a new function.
Looks mathematica wants to prevent a kind of collision of variable names. But I think such thoughtfulness is unnecessary for this case. Function[x, 2 x + 1] just represents a function that maps a thing to its double plus one. After completing interpretation of the function, we can completely forget the fact : 'the variable used in Function[ ] was x'. What do you think ?
:=
, which redefines the anonymous function every time you callF
. The problem probably goes away by using an immediate assignment=
. Here's a tutorial. $\endgroup$Function
comes with risks that are usually easily avoided by sticking with slot notation. $\endgroup$