The $g(a) = a^x$ is invalid, because the argument is $a$ and $x$ appears as a free variable. If a function's body has a free variable, that has to be defined somewhere as a constant or a globally understood parameter.
A function cannot have a free variable that refers to the argument of another function.
The correct version of $g$ is $g(a) = a^a$.
The function $g$ raises the argument to itself; it does not raise its argument to $x$. $x$ is a dummy variable in the definition of $f(x)$, which is another function. And what if the dummy variable in $f(x)$ is some thing else, like $f(b) = b$?
Think about how this would work in some functional programming language in computing:
return pow(a, x)
g is referring to
x, but the only
x that we see is local in another function's scope! There is no visible binding for
x in the scope of
g's body, so it is a free variable. Moreover, we can edit
without changing the meaning of the program. So now there isn't an
In mathematics, functions can often be regarded as macros! But they are not blind, textual macros; they are hygienic macros. Substitution in math formulas is on structure not text, and it obeys scoping rules by maintaining lexical transparency.
For instance consider this following student reasoning error. Let $f(x) = x\times x$ and let $g(y) = y + y$. Therefore, since manipulation of mathematic formulas is just dumb textual substitution like preprocessor macros in the C language, $f(g(3)) = 3 + 3 * 3 + 3$, and so the value is $f(g(3)) = 3 + 9 + 3 = 15$. And in general $f(g(z)) = z^2 + 2z$. What is wrong?
Since substitution in math formulas follows structure, $f(g(z))$ cannot be $z^2 + 2z$. $f$ multiplies whatever object comes out of $g$ without breaking apart its syntax, and so the necessary parentheses have to be shown when it is all put together: $f(g(z)) = (z + z)(z + z) = 4z^2$.
The rule of lexical transparency is this: that when we substitute a formula $\alpha$ as a replacement for some symbol in another formula $\beta$, all of the symbols in $\beta$, all of the symbols in $\alpha$ continue to have exactly the same meaning. Even if $\beta$ has symbols which have the same names, those symbols do not capture any of the references in $\alpha$.
To prevent confusion, if such a situation arises where there is a confusion between symbols in an inserted formula and in the target of insertion, we should perform a variable renaming.