(bc.info)Functions


Next: Examples Prev: Statements Up: Top
Enter node , (file) or (file)node

5 Functions
***********

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.  All parameters are call by value parameters.
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 value 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.

   Several extensions have been added to functions.  First, 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); }

   Functions may be defined as 'void'.  A void funtion returns no value
and thus may not be used in any place that needs a value.  A void
function does not produce any output when called by itself on an input
line.  The key word 'void' is placed between the key word 'define' and
the function name.  For example, consider the following session.

     define py (y) { print "--->", y, "<---", "\n"; }
     define void px (x) { print "--->", x, "<---", "\n"; }
     py(1)
     --->1<---
     0
     px(1)
     --->1<---

   Since 'py' is not a void function, the call of 'py(1)' prints the
desired output and then prints a second line that is the value of the
function.  Since the value of a function that is not given an explicit
return statement is zero, the zero is printed.  For 'px(1)', no zero is
printed because the function is a void function.

   Also, call by variable for arrays was added.  To declare a call by
variable array, the declaration of the array parameter in the function
definition looks like "'*'NAME'[]'".  The call to the function remains
the same as call by value arrays.


automatically generated by info2www version 1.2.2.9