MPE/iX Shell and Utilities Reference Manual, Vol 1

bc(1) MPE/iX Shell and Utilities bc(1)
To activate the subprogram you use a function call. This has the form
name(expression[,expression] ...)
where name is the name of the function, and the expressions are argument values for the func-
tion. A function call can be used anywhere you might use any other expression. The value of
the function call is the value that the function returns. For example, with the function
f_to_c() described earlier, f_to_c(41) has the value 5 (since 41 Fahrenheit is equiva-
lent to 5 Celsius).
The general form of a function definition is
define name([parameter][,parameter]...) {
auto local, local, ...
statement
statement
...
}
The parameters on the first line may be variable names or array names. Array names are indi-
cated by putting square brackets after them. For example, if cmpvec() is a function that
compares two vectors, the function definition might start with
define cmpvec(a[],b[]) {
Parameters do not conflict with arrays or variables of the same name. For example, you may
have a parameter named a inside a function, and a variable named a outside, and the two are
considered entirely separate entities. Assigning a value to the variable does not change the
parameter and vice versa. All parameters are passed by value. This means that a copy is made
of the argument value and is assigned to the formal parameter. This also applies to arrays. If
you pass an array to a function, a copy is made of the whole array, so any changes made to the
array parameter don’t affect the original array.
A function may not need any arguments. In this case, the define line does not have any
parameters inside the parentheses, as in
define f() {
The auto statement declares a sequence of local variables. When a variable or array name
appears in an auto statement, the current values of those items are saved and the items are
initialized to zero. For the duration of the function, the items have their new values. When
the function terminates, the old values of the items are restored. Note, however, that bc uses
dynamic scoping rules, unlike C which uses lexical scoping rules (see the NOTES section for
more details).
Commands and Utilities 1-57