Take the following Lua code (which I use because Lua is compiled to bytecode before being interpreted):
local myVar = "h";
local function printer()
print(myVar)
end;
printer();
myVar = 7;
printer();
The output to this is h, then, on a new line, 7.
Due to the dynamic typing of the language, I imagine the variable must be re-allocated in memory due to the change in data type. Proceeding with this assumption, myVar must refer to different places at different parts of the script. If that is the case, it makes sense to me that there must be two versions of printer compiled: one pre-change, and one post-change.
I also considered that each variable may have some memory location allocated to it, and that the given memory location can be checked to find the currently allocated location for the variable's value. If this is the case, I suppose reference types like tables have a reference stored to at the referred-to location (a double-reference)?
So, is a function compiled for each different version of it that might run? Are variable location changes tracked with a pointer? Or is some other process going on here?