def fn - define a user function


def fnName[(Arg[, Arg]. . .)] = Expr
def fnName[(Arg[, Arg]. . .)]

[[let] fnName = Expr]

[exit def]

end def


Def fn defines a user function whose name begins with "fn".  The function name must be a valid variable name.  Later in the program, the function thus defined may be executed by specifying arguments and referencing the function name with a function call in an expression. 

An Arg is a variable name.  An Arg in the definition of a function is known as a formal parameter to the function and will be replaced with an actual parameter when the call to the function occurs.  The formal parameters are associated with the actual parameters positionally; i.e., the first formal parameter is associated with the first actual parameter, the second formal parameter with the second actual parameter, etc. The parameters are passed by value.  When the function is executed, the actual parameters are copied before they are used, so these copies may be changed inside the function without effecting the actual parameters. 

The methods of defining a user function are two. 

With the single line method, the definition is limited to a single expression.  When the function is referenced in an expression in the body of the program, the arguments are evaluated and passed to the function.  Within the function, the expression, Expr, is evaluated.  The value of Expr is converted to the type of the function, if necessary, and assigned as the value of the function in the same manner as a let statement (see let).  The value of the function is returned and used in evaluating the expression that referenced the function. 

With the multiline method, the definition extends to the end def statement.  Note that the multiline method is distinguished from the single line method by the omission of the equal sign and expression in the def fn statement.  When the function is referenced in an expression in the body of the program, the arguments are evaluated and passed to the function.  Within the function, execution begins with the first statement after the def fn statement and the value of the function is initially set to zero or the null string (depending on the type of the function).  The value of the function may be assigned a new value by the execution of a let statement in the left-hand side of which appears the name of the function.  Execution of the function continues until the appearance of an exit def or the end def statement.  The value of the function is returned and used in evaluating the expression that referenced the function. 

In general, the statements that may appear in a multiline function definition are the same as those allowed in the body of the program with the following notable exceptions: The on error goto statement, if used, must reference a line number or label outside any function or subprogram.  All other statements must not reference a line number or label outside the function. 

Regardless of which of the two methods is used to define the function, note that defining formal parameters actually redefines the meaning of those variable names within the function.  A variable name with one meaning outside the function will have new meaning within the function although the name looks the same.  If this is not kept in mind, one might write a function using the same name for two different meanings unaware that the compiler applies only one meaning, namely, as a formal parameter.  A simple way to avoid this problem is to select variable names for the formal parameters that do not conflict with names used outside the function. 

Sometimes temporary variables are needed within a function.  Preferably these variables would be free from name conflicts with variables outside the function.  Variables that are private to the function and free from conflicts may be defined with the static statement (see static). 

A function definition may not appear within a function or subprogram definition.  A function may not be recursive, i.e., a function definition may not contain a reference to the same function.  Loop, conditional and function and subprogram definition statements may not violate the boundaries of a function definition, i.e., a multiline construct may not begin outside a definition and end inside or vice versa

A functionís definition must appear before its references.  Function definitions are not available between modules.  If several modules need the same function, each module must include a definition. 


The program
	   pi = 3.1459265
	70 def fncylsurf(r, l) = 2 * pi * r * (r + l)
	   input "radius, length? ", radius, length
	   print "Surface area of the cylinder is";
	90 print fncylsurf(radius, length)
	radius, length?
Suppose you respond with 3 and 7. 
	radius, length? 3, 7
The output would be
	Surface area of the cylinder is 188.7556
Line 70 defines the function fncylsurf, which calculates the surface area of a cylinder with radius r and length l.  The function is called in line 90. 

This program computes the chances of winning the Ohio lottery:

	defdbl a-z
	def fncomb(i, j)
		static tot

		tot = 1
		while i > 0
			tot = tot * j / i
			i = i - 1: j = j - 1
		fncomb = tot
	end def
	def fnprob(i, j) = fncomb(i, 6.0) * fncomb(j, 38.0)_
				/ fncomb(i + j, 44.0)
	for i = 3 to 6
		print "Your chances of matching" i;
		print "are one in" int(1 / fnprob(i, 6 - i) + 0.5)
	next i
	print "Support your local Board of Education"
This is its output:
	Your chances of matching 3 are one in 42 
	Your chances of matching 4 are one in 669 
	Your chances of matching 5 are one in 30961 
	Your chances of matching 6 are one in 7059052 
	Support your local Board of Education


let, static

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber