MPE/iX Shell and Utilities Reference Manual, Vol 1

bc(1) MPE/iX Shell and Utilities bc(1)
A return statement terminates a function, even if there are more statements left in the func-
tion. For example,
define abs(i) {
if (i < 0) return (-i)
return (i)
}
is a function that returns the absolute value of its argument. If i is less than zero, the function
takes the first return; otherwise, it takes the second.
A function can also terminate by performing the last statement in the function. If so, the result
of the function is zero. The function sum_c() is an example of a function that doesn’t have
a return statement. The function doesn’t need a return statement, because its work is to
calculate the external array c, not to calculate a single value. Finally, if you want to return
from a function, but not return a value you may use
return ()
or simply
return
If there are no parameters to the return statement, a default value of zero is returned.
Built-In Functions
bc has a number of built-in functions that perform various operations. These functions are
similar to user-defined functions with the exception that you don’t have to define them your-
self — they are already set up for you. These functions are:
length(expression)
calculates the total number of decimal digits in expression. This includes digits both
before and after the decimal point. The result of length() is an integer. For example,
length(123.456) returns 6.
scale(expression)
returns the scale of expression. For example, scale(123.456) returns 3. The result
of scale() is always an integer. Subtracting the scale of a number from the length of a
number lets you determine the number of digits before the decimal point.
sqrt(expression)
calculates the square root of the value of expression. The result is truncated in the least
significant decimal place (not rounded). The scale of the result is the scale of expres-
sion, or the value of scale(), whichever is larger.
Commands and Utilities 1-59