Scope modifiers: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: Added bc)
(→‎{{header|bc}}: Rephrased second paragraph)
Line 150: Line 150:
All identifiers are global by default (except function parameters which are local to the function). There is one scope modifier: <code>auto</code>. All identifiers following the <code>auto</code> statement are local to the function and it must be the first statement inside the function body if it is used. Furthermore there can only be one <code>auto</code> per function.
All identifiers are global by default (except function parameters which are local to the function). There is one scope modifier: <code>auto</code>. All identifiers following the <code>auto</code> statement are local to the function and it must be the first statement inside the function body if it is used. Furthermore there can only be one <code>auto</code> per function.


It's important to understand, that all function parameters and local identifiers are pushed onto a stack when entering the function and thus hide the values of identifiers with the same name from the outer scope. They are popped from the stock when the functions returns. Thus it is possible that a function which is called from another function has access to the local identifiers and parameters of its caller if itself doesn't use the same name as a local identifier.
One can think of each identifier as a stack. Function parameters and local identifiers are pushed onto the stack and shadow the values of identifiers with the same names from outer scopes. They are popped from the stack when the function returns. Thus a function that is called from another function has access to the local identifiers and parameters of its caller if itself doesn't use the same name as a local identifier/parameter. In other words, always the innermost value (the value at the top of the stack) for each identifier is visible, regardless of the scope level where it is accessed.


<lang bc>define g(a) {
<lang bc>define g(a) {