       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:=
Clear[f, g, h]

In:=
f[x_] := x^2 + 2

In:=
g[x_] := Sin[x]

In:=
h[x_] := Module[{}, {f[x], g[x], g[f[x]]}]

In:=
h[Pi]

Out=
{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:=
Clear[f, g, h]

In:=
f[x_] := Module[{y}, y = x^2 + 2;
Print[StringJoin["f(", ToString[x], ")=",
ToString[y]]]; y]

In:=
g[x_] := Module[{y}, y = Sin[x];
Print[StringJoin["g(", ToString[x], ")=",
ToString[y]]]; y]

In:=
h[x_] := Module[{}, f[x]; g[x]; g[f[x]]]

In:=
h

From In:=
"f(2)=6"

From In:=
"g(2)=Sin"

From In:=
"f(2)=6"

From In:=
"g(6)=Sin"

Out=
Sin

In:=
main[x_] := Module[{i}, i = 2; While[i > 0,
f[x]; g[x]; g[f[x]]; i--; ]]

In:=
main

From In:=
"f(3)=11"

From In:=
"g(3)=Sin"

From In:=
"f(3)=11"

From In:=
"g(11)=Sin"

From In:=
"f(3)=11"

From In:=
"g(3)=Sin"

From In:=
"f(3)=11"

From In:=
"g(11)=Sin"

Best regards,
/J.M.

```

• Prev by Date: Re: Don't understand behaviour of Solve[]
• Next by Date: Re: GroebnerBasis (was Re: Documentation)
• Previous by thread: Re: Problem with multiple function calling from a novice...
• Next by thread: GroebnerBasis (was Re: Documentation)