Re: Problem with multiple function calling from a novice...
- To: mathgroup at smc.vnet.net
- Subject: [mg58589] Re: Problem with multiple function calling from a novice...
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 9 Jul 2005 04:07:50 -0400 (EDT)
- Organization: The Open University, Milton Keynes, England
- References: <dal1tq$ibc$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
sami wrote: > Hi all. I have a small problem, and I would like some help. > > Suppose I define a few functions, say a,b,c etc. > > Question 1: How can I call them from inside a program, so that they > ALL > show what they evaluate in turn? (Not just the last one, but each > one.) > > Question 2: How can I modify an If...Then clause of the form: > > If[ <Condition> , a;b;c;...etc.] > > or a While loop: > > While[ <Condition> , a;b;c;...etc.] > > so that, again, they ALL show what they evaluate in turn? (Again, not > just > the > last one like in this case). > > Thank you > > Sami > Hi Sami, At least to methods come up in my mind. First, you can write your functions as usual and wrap them within a list in the body of the main function. In[1]:= Clear[f, g, h] In[2]:= f[x_] := x^2 + 2 In[3]:= g[x_] := Sin[x] In[4]:= h[x_] := Module[{}, {f[x], g[x], g[f[x]]}] In[5]:= h[Pi] Out[5]= {2 + Pi^2, 0, Sin[2 + Pi^2]} Second, to get more control on what the functions are displaying and to use them within a *If* or *While* loop, it is better to use some *Print* command within each function whenever you want to display some intermediate results. In[6]:= Clear[f, g, h] In[7]:= f[x_] := Module[{y}, y = x^2 + 2; Print[StringJoin["f(", ToString[x], ")=", ToString[y]]]; y] In[8]:= g[x_] := Module[{y}, y = Sin[x]; Print[StringJoin["g(", ToString[x], ")=", ToString[y]]]; y] In[9]:= h[x_] := Module[{}, f[x]; g[x]; g[f[x]]] In[10]:= h[2] From In[10]:= "f(2)=6" From In[10]:= "g(2)=Sin[2]" From In[10]:= "f(2)=6" From In[10]:= "g(6)=Sin[6]" Out[10]= Sin[6] In[11]:= main[x_] := Module[{i}, i = 2; While[i > 0, f[x]; g[x]; g[f[x]]; i--; ]] In[12]:= main[3] From In[12]:= "f(3)=11" From In[12]:= "g(3)=Sin[3]" From In[12]:= "f(3)=11" From In[12]:= "g(11)=Sin[11]" From In[12]:= "f(3)=11" From In[12]:= "g(3)=Sin[3]" From In[12]:= "f(3)=11" From In[12]:= "g(11)=Sin[11]" Best regards, /J.M.