MPE/iX Shell and Utilities Reference Manual, Vol 1

bc(1) MPE/iX Shell and Utilities bc(1)
PORTABILITY
POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems.
The –i option, the && and  operators, the if ... else ... statement, the print state-
ment, the sh statement, and the optional parentheses in the return statement are exten-
sions to the
POSIX standard.
NOTES
This section describes some additional details about bc that may be useful to know.
Unlike the C language which uses lexical scoping rules, bc uses dynamic scoping. This is
most easily explained with an example:
a=10
define f1() {
auto a;
a = 13;
return (f2())
}
define f2() {
return (a)
}
f1()
13
f2()
10
If f1() is called, bc displays the number 13, instead of the number 10. This is because
f1() hides away the old (global) value of a and then sets it to 13. When f2() refers to a it
sees the variable dynamically created by f1() and so displays 13. When f1() returns, it
restores the old value of a. When f2() is called directly, instead of through f1() it sees the
global value for a and displays 10. The corresponding C code displays 10 in both cases.
Numbers are stored as strings in the program and converted into numbers each time they are
used. This is important because the value of a constant number may change depending on the
setting of the ibase variable. For example, suppose the following instructions are given to
bc:
define ten() {
return (10)
}
ten()
10
1-66 Commands and Utilities