3
$\begingroup$
ver1 = 2 x
verf[x_] := ver1
verf[3]

Result : 2 x

Expected Result : 6

In the above code sample, ver1 can change to expressions (4 x + 3,3 x etc)

What is wrong in the code? How can I achieve my objective?

$\endgroup$

marked as duplicate by rhermans, xyz, Mr.Wizard Oct 10 '15 at 9:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Please also see: (11461) $\endgroup$ – Mr.Wizard Oct 10 '15 at 9:59
  • $\begingroup$ I missed 'Evaluate' in my code .. as suggested by @Enrique Pérez Herrero $\endgroup$ – rainversion_3 Oct 11 '15 at 12:53
  • 1
    $\begingroup$ If that is a solution for you simply use = (Set) rather than := (SetDelayed) and leave out Evaluate, for the same effect. I believed that you were looking for a somewhat different evaluation behavior as described in the marked duplicate; even if you are not you may wish to familiarize yourself with that. $\endgroup$ – Mr.Wizard Oct 12 '15 at 6:59
1
$\begingroup$

If you do not want to use set delayed in ver1, this is ver1[x_]:= 2 x, you need to evaluate ver1 when verf is defined:

ver1 = 2 x
verf[x_] := Evaluate[ver1]
verf[3]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.