Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have the following code:

fbasic = A + R + Z*Z *Z*Z;
f[A_, Z_, R_] := fbasic + R;
f[0, 2, 3]

The answer I am getting is

3 + A + R + Z^4

Which is not what I want because the fbasic is not replaced by the expression. How can I do that?

share|improve this question
    
Try using Set rather than SetDelayed. –  b.gatessucks Aug 18 '13 at 14:45
    
Thanks so much. It works. Could you give me more information about the difference between := and =? I really appreciate your help. –  user8551 Aug 18 '13 at 14:52
    
By the way, if I use Set rather than SetDelayed, how can I call this function later? Because "fbasic"can be an arbitrary function. –  user8551 Aug 18 '13 at 15:04
    

1 Answer 1

For example, use Set Delayed and arguments

fbasic[a_, z_, r_] := a + r + z^4;
f[a_, z_, r_] := fbasic[a, z, r] + r;
f[0, 2, 3]

Another option is:

fbasic = aa + rr + zz^4;
f[a_, z_, r_] := Evaluate[fbasic /. {rr -> r, aa -> a, zz -> z}] + r;
f[0, 2, 3]

Or:

fbasic = aa + rr + zz^4;
f[a_, z_, r_] := Block[{rr = r, aa = a, zz = z}, fbasic + r]
f[0, 2, 3]
share|improve this answer
    
Thanks for your kind help. But in my code, there is a loop. Sometimes I don't the expression of "fbasic" which is calculated by the previous one. How I define this kind of function? –  user8551 Aug 18 '13 at 15:12
1  
@user8551 That seems another kind of question. Could you edit your question specifying the problem a little bit more? –  belisarius Aug 18 '13 at 15:18
    
Omit Evaluate? –  Michael E2 Aug 18 '13 at 15:22
    
The concept of my code is: I write a function to calculate the multidimensional numerical integration.The input is a function. The output is the value which is the numerical integration. The first part of the code is 'FNintegration[fbasic_] := f[A_, Z_, R_] := fbasic;.....' . fbasic is an arbitrary function of A,Z and R.I need to get the value with different A Z and R. That is why I define another function f[A_, Z_, R_]. I want to call this function later. Thanks for your kind help. –  user8551 Aug 19 '13 at 2:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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