Skip to main content
Tweeted twitter.com/#!/StackMma/status/560794800962932737
Minor clean-up
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

I define a variable as local forto a module BUT then the module uses its global value! whyWhy?

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a, h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following alternatives work and return correctly 16

Module[{a, h}, a[h_] := h^2; a[4]]
Module[{a, h}, a[h]=a[h] = h^2; a[4]]
Module[{a, j}, a[j_]=a[j_] = j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a, h}, a[h_]=a[h_] = h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

I define a variable as local for a module BUT then the module uses its global value! why?

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a,h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following alternatives work and return correctly 16

Module[{a,h}, a[h_]:= h^2; a[4]]
Module[{a,h}, a[h]= h^2; a[4]]
Module[{a,j}, a[j_]= j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a,h}, a[h_]= h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

I define a variable as local to a module BUT then the module uses its global value! Why?

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a, h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following alternatives work and return correctly 16

Module[{a, h}, a[h_] := h^2; a[4]]
Module[{a, h}, a[h] = h^2; a[4]]
Module[{a, j}, a[j_] = j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a, h}, a[h_] = h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

I added another alternative that works, namely a[h] instead of a[h_]
Source Link

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a,h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following three alternatives work and return correctly 16

Module[{a,h}, a[h_]:= h^2; a[4]]
Module[{a,h}, a[h]= h^2; a[4]]
Module[{a,j}, a[j_]= j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a,h}, a[h_]= h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a,h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following three alternatives work and return correctly 16

Module[{a,h}, a[h_]:= h^2; a[4]]
Module[{a,j}, a[j_]= j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a,h}, a[h_]= h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a,h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following alternatives work and return correctly 16

Module[{a,h}, a[h_]:= h^2; a[4]]
Module[{a,h}, a[h]= h^2; a[4]]
Module[{a,j}, a[j_]= j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a,h}, a[h_]= h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a,h}, a[h_]= h^2, a[4]]

output = 25

h = 5;

Module[{a,h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following three alternatives work and return correctly 16

Module[{a,h}, a[h_]:= h^2, a[4]] Module[{a,j}, a[j_]= j^2, a[4]] (* with j a variable not previously defined *) Block[{a,h}, a[h_]= h^2, a[4]]

Module[{a,h}, a[h_]:= h^2; a[4]]
Module[{a,j}, a[j_]= j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a,h}, a[h_]= h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a,h}, a[h_]= h^2, a[4]]

output = 25

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following three alternatives work and return correctly 16

Module[{a,h}, a[h_]:= h^2, a[4]] Module[{a,j}, a[j_]= j^2, a[4]] (* with j a variable not previously defined *) Block[{a,h}, a[h_]= h^2, a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

I have a question about modules and local variables.

Here's my example:

h = 5;

Module[{a,h}, a[h_]= h^2; a[4]]

(*Out[2] = 25*)

I expected the module to return 16 and not 25! I believed h to be a LOCAL variable of the module!

I know that the following three alternatives work and return correctly 16

Module[{a,h}, a[h_]:= h^2; a[4]]
Module[{a,j}, a[j_]= j^2; a[4]]  (* with j a variable not previously defined *)
Block[{a,h}, a[h_]= h^2; a[4]]

Question: why does the module behave like that? i.e. why does it return 25 and not 16?

P.S. I am running out of variables and I am then resorting to modules to avoid errors due to variables defined more than once. I played a bit with modules to check their properties and I ran in this example.

Thanks for your help!

Source Link
Loading