MPE/iX Shell and Utilities Reference Manual, Vol 1
bc(1) MPE/iX Shell and Utilities bc(1)
For example,
define addarr(a[],l) {
auto i, s
for (i=0;i<l;++i) s += a[i]
return (s)
}
is a function that adds the elements in an array. The argument l stands for the number of ele-
ments in the array. The function uses two local names: a variable named i and a variable
named s. These variables are local to the function addarr() and are unrelated to objects of
the same name outside the function (or in other functions). Objects named in an auto state-
ment are called autos. Autos are initialized to zero each time the function is called. Thus the
sum s is set to zero each time this function is called. You may also have local arrays, which
are specified by placing square brackets after the array name in the auto statement.
define func_with_local_array() {
auto local_array[];
for(i=0; i<100; i++) local_array[i] = i*2
}
This example defines a local array called local_array. Local arrays start out with no ele-
ments in them.
If a function refers to an object that is not a parameter and not declared auto, the object is
assumed to be external. External objects may be referred to by other functions or by state-
ments which are outside of functions. For example,
define sum_c(a[],b[],l) {
auto i
for (i=0;i<l;++i) c[i] = a[i] + b[i]
}
references an external array named c which is the element-by-element sum of two other
arrays. If c did not exist prior to calling sum_c(), it is created dynamically. Once the pro-
gram has called sum_c(), statements in the program or in functions can refer to array c.
Functions usually require a return statement. This has the form
return (expression)
The expression is evaluated and used as the result of the function. The expression must have a
single numeric value; it cannot be an array.
1-58 Commands and Utilities