About your question regarding the definition of the type of local variables in Compile, Compile has an optional third argument that allows you do this in the same manner you specify arguments. It helps the compiler solve some type ambiguity issues sometimes as by default a local variable is considered a Real number.
This can be the case if a local variable is the result of another external function and the compiler cannot infer properly the type of the result of this external function. For example
xfunctionf = Compile[{x, y}, {x, y}]
ff = Compile[{x,y}, x]
g =
Compile[x,
Module[{y={}y},
y AppendTo[y= h[3.,x] 4.];
] y[[1]] = 2+x;
, y
]
,
{{yh[__], _Real, 1}}
]
h = f;
g[1.] (*works*)
xfunction[1h = ff;
g[1.] (*error*)
Without {{h[__], _Real, 1}} the compilation of g would produce an error.