(bc.info.gz) Functions
Info Catalog
(bc.info.gz) Statements
(bc.info.gz) Top
(bc.info.gz) Examples
Functions
*********
Menu
* Math Library Functions
Functions provide a method of defining a computation that can be
executed later. Functions in `bc' always compute a value and return it
to the caller. Function definitions are "dynamic" in the sense that a
function is undefined until a definition is encountered in the input.
That definition is then used until another definition function for the
same name is encountered. The new definition then replaces the older
definition. A function is defined as follows:
`define' NAME `(' PARAMETERS `)' `{' NEWLINE
AUTO_LIST STATEMENT_LIST `}'
A function call is just an expression of the form "`name'
`('PARAMETERS`)'".
Parameters are numbers or arrays (an extension). In the function
definition, zero or more parameters are defined by listing their names
separated by commas. Numbers are only call by value parameters.
Arrays are only call by variable. Arrays are specified in the
parameter definition by the notation "NAME`[ ]'". In the function
call, actual parameters are full expressions for number parameters.
The same notation is used for passing arrays as for defining array
parameters. The named array is passed by variable to the function.
Since function definitions are dynamic, parameter numbers and types are
checked when a function is called. Any mismatch in number or types of
parameters will cause a runtime error. A runtime error will also occur
for the call to an undefined function.
The AUTO_LIST is an optional list of variables that are for "local"
use. The syntax of the auto list (if present) is "`auto' NAME, ... ;".
(The semicolon is optional.) Each NAME is the name of an auto
variable. Arrays may be specified by using the same notation as used
in parameters. These variables have their values pushed onto a stack
at the start of the function. The variables are then initialized to
zero and used throughout the execution of the function. At function
exit, these variables are popped so that the original value (at the
time of the function call) of these variables are restored. The
parameters are really auto variables that are initialized to a value
provided in the function call. Auto variables are different than
traditional local variables because if function A calls function B, B
may access function A's auto variables by just using the same name,
unless function B has called them auto variables. Due to the fact that
auto variables and parameters are pushed onto a stack, `bc' supports
recursive functions.
The function body is a list of `bc' statements. Again, statements
are separated by semicolons or newlines. Return statements cause the
termination of a function and the return of a value. There are two
versions of the return statement. The first form, "`return'", returns
the value 0 to the calling expression. The second form, "`return' (
EXPRESSION )", computes the value of the expression and returns that
value to the calling expression. There is an implied "`return' (0)" at
the end of every function. This allows a function to terminate and
return 0 without an explicit `return' statement.
Functions also change the usage of the variable IBASE. All
constants in the function body will be converted using the value of
IBASE at the time of the function call. Changes of IBASE will be
ignored during the execution of the function except for the standard
function `read', which will always use the current value of IBASE for
conversion of numbers.
As an extension, the format of the definition has been slightly
relaxed. The standard requires the opening brace be on the same line
as the `define' keyword and all other parts must be on following lines.
This version of `bc' will allow any number of newlines before and after
the opening brace of the function. For example, the following
definitions are legal.
define d (n) { return (2*n); }
define d (n)
{ return (2*n); }
Info Catalog
(bc.info.gz) Statements
(bc.info.gz) Top
(bc.info.gz) Examples
automatically generated byinfo2html