x = 5*y
function1[y_] := x
function1[5]
Am I wrong in thinking that function1[5] should equal 25? Instead this comes out.
5y
5y
Am I wrong in thinking that function1[5] should equal 25? Instead this comes out.
|
||||
|
Without going into too much detail, this is a result of the way pattern matching is carried out. When you write the rule
(*=> {f[3],{x,3},{x,5 y},{5 y,3}} *) The way to get around this is to get x to evaluate before you define the rule, which you can do for instance by passing it through an anonomous function:
If you want to see why this works out you can see that at the time the rule definition is evaluated x has been fully replace by using
|
|||||||
|
|
As already explained the named pattern As a point of reference this works:
The value of
An approach that will let you change the value of
|
|||
|
|
|
What you want do is this:
The way you define functions in Mathematica is by defining patterns, which is indicated here by |
|||
|
_(likey_in your example) to define the variable of a function. – b.gatessucks Sep 12 '12 at 13:57