1 1 5 Functions 1 *********** 1
1 · Math Library Functions 1 1 Functions provide a method of defining a computation that can be 1 executed later. Functions in 'bc' always compute a value and return it 1 to the caller. Function definitions are "dynamic" in the sense that a 1 function is undefined until a definition is encountered in the input. 1 That definition is then used until another definition function for the 1 same name is encountered. The new definition then replaces the older 1 definition. A function is defined as follows: 1 1 define NAME ( PARAMETERS ) { NEWLINE 1 AUTO_LIST STATEMENT_LIST } 1 1 A function call is just an expression of the form "'name' 1 '('PARAMETERS')'". 1 1 Parameters are numbers or arrays (an extension). In the function 1 definition, zero or more parameters are defined by listing their names 1 separated by commas. All parameters are call by value parameters. 1 Arrays are specified in the parameter definition by the notation "NAME'[ 1 ]'". In the function call, actual parameters are full expressions for 1 number parameters. The same notation is used for passing arrays as for 1 defining array parameters. The named array is passed by value to the 1 function. Since function definitions are dynamic, parameter numbers and 1 types are checked when a function is called. Any mismatch in number or 1 types of parameters will cause a runtime error. A runtime error will 1 also occur for the call to an undefined function. 1 1 The AUTO_LIST is an optional list of variables that are for "local" 1 use. The syntax of the auto list (if present) is "'auto' NAME, ... ;". 1 (The semicolon is optional.) Each NAME is the name of an auto variable. 1 Arrays may be specified by using the same notation as used in 1 parameters. These variables have their values pushed onto a stack at 1 the start of the function. The variables are then initialized to zero 1 and used throughout the execution of the function. At function exit, 1 these variables are popped so that the original value (at the time of 1 the function call) of these variables are restored. The parameters are 1 really auto variables that are initialized to a value provided in the 1 function call. Auto variables are different than traditional local 1 variables because if function A calls function B, B may access function 1 A's auto variables by just using the same name, unless function B has 1 called them auto variables. Due to the fact that auto variables and 1 parameters are pushed onto a stack, 'bc' supports recursive functions. 1 1 The function body is a list of 'bc' statements. Again, statements 1 are separated by semicolons or newlines. Return statements cause the 1 termination of a function and the return of a value. There are two 1 versions of the return statement. The first form, "'return'", returns 1 the value 0 to the calling expression. The second form, "'return' ( 1 EXPRESSION )", computes the value of the expression and returns that 1 value to the calling expression. There is an implied "'return' (0)" at 1 the end of every function. This allows a function to terminate and 1 return 0 without an explicit 'return' statement. 1 1 Functions also change the usage of the variable IBASE. All constants 1 in the function body will be converted using the value of IBASE at the 1 time of the function call. Changes of IBASE will be ignored during the 1 execution of the function except for the standard function 'read', which 1 will always use the current value of IBASE for conversion of numbers. 1 1 Several extensions have been added to functions. First, the format 1 of the definition has been slightly relaxed. The standard requires the 1 opening brace be on the same line as the 'define' keyword and all other 1 parts must be on following lines. This version of 'bc' will allow any 1 number of newlines before and after the opening brace of the function. 1 For example, the following definitions are legal. 1 1 define d (n) { return (2*n); } 1 define d (n) 1 { return (2*n); } 1 1 Functions may be defined as 'void'. A void funtion returns no value 1 and thus may not be used in any place that needs a value. A void 1 function does not produce any output when called by itself on an input 1 line. The key word 'void' is placed between the key word 'define' and 1 the function name. For example, consider the following session. 1 1 define py (y) { print "--->", y, "<---", "\n"; } 1 define void px (x) { print "--->", x, "<---", "\n"; } 1 py(1) 1 --->1<--- 1 0 1 px(1) 1 --->1<--- 1 1 Since 'py' is not a void function, the call of 'py(1)' prints the 1 desired output and then prints a second line that is the value of the 1 function. Since the value of a function that is not given an explicit 1 return statement is zero, the zero is printed. For 'px(1)', no zero is 1 printed because the function is a void function. 1 1 Also, call by variable for arrays was added. To declare a call by 1 variable array, the declaration of the array parameter in the function 1 definition looks like "'*'NAME'[]'". The call to the function remains 1 the same as call by value arrays. 1