Skip to main content

functions - Refering to global variables inside modules


How do I refer to global variables inside modules if the same name is used for a local one? In code, I want that


proc[a_] := Module[{b},
b = a;
{"b" -> b}
]
b /. proc[1]

returns 1. Here "b" is to be replaced by the properly escaped global variable name.




Answer



Using Symbol is one way:


b = 10;
Module[{b},
b = 5;
Symbol["b"]
]
(* Out: 10 *)

Your example:



proc[a_] := Module[{b}, b = a;
{Symbol["b"] -> b}]
b /. proc[1]
(* Out: 1 *)

Comments