Re: Using 'Module' inside 'Compile'

• To: mathgroup at smc.vnet.net
• Subject: [mg112174] Re: Using 'Module' inside 'Compile'
• From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
• Date: Fri, 3 Sep 2010 06:09:50 -0400 (EDT)

```Hi,

I assume when tmp is not used bevor, then it is *declared* and
*initialized* inside the if block like in C

f1() {
double x = 4.0;
if(3>2) {
double tmp = x;
printf("...",tmp);
}
printf("...",tmp);
}

so why don't you try

f1 = Compile[{}, Module[{x, temp = 0.0}, x = 4.;
If[3 > 2, temp = x; Print["temp in 'if' statement: ", temp]];
Print["temp outside 'if' statement: ", temp];];];

?

Cheers
Patrick

On Thu, 2010-09-02 at 02:31 -0400, Scott D. Kasen wrote:
> While I've worked with Mathematica for quite some time, I'm first
> learning the Compile function.  Keeping it brief...
>
> Compile the function...
>
> f1 = Compile[{},
>    Module[{x, temp},
>      x = 4.;
>      If[3 > 2, temp = x; Print["temp in 'if' statement: ", temp]];
>      Print["temp outside 'if' statement: ", temp];
>      ];
>    ];
>
> Calling the function...
>
> f1[]
>
> Produces the following output:
>
> temp in 'if' statement: 4.
> temp outside 'if' statement: temp\$3107
>
> First, why does the function create a new local variable temp\$3107
> outside of the 'If' statement?  Second, how can I bring the first value
> for "temp" outside of the if statement?
>