Calculus Computer Lab
Math 190


News

Lab Schedule

QuickBasic Notes

Fortran Notes

Tutor Room

Winplot Manual

Lab Home

xxxxxxxxxxxxxxxxxx

BASIC LESSON 7

Lesson Topics
DEF fn (Single Line)Permutations
DEF fn (Multiple Line)INT
Factorials
View DemosDownload Demos
# 1 # 2 # 3 # 4 # 5 # 6 # 7 # 1 # 2 # 3 # 4 # 5 # 6 # 7
Main Basic Page

DEF fn (Single Line Version)

The DEF fn command is used for defining functions.

Examples :

DEF fnperim (A, B) = 2 * A + 2 * B
DEF fnf (X) = X ^ 2 + 3 * X + 5
DEF fnarea (R) = 3.14 * R ^ 2
The three examples define the three functions
P(a,b) = 2a + 2b ,
f(x) = x2 + 3x + 5 ,
A(r) = 3.14r2 .
The Basic names for the functions are "fnperim", "fnf", and "fnarea", respectively. The general format of a single line DEF fn statement is

DEF fnfunctionname (A, B, C, …) = formula .

In place of "functionname" insert your own name for the function, in place of "formula" insert the formula for computing the function, and instead of A, B, C, etc., list the variables upon which the function depends.

After defining a function, you may later ask Basic to use your definition to plug values into the function. With the above fnf example, the statement

Y = fnf(4)

will assign Y the value fnf (4) = 4 ^ 2 + 3 * 4 + 5 = 33. The command Y = fnf (Z) assigns Y the value of fnf (X) with X equal to the present value of Z.

You can specify a type for a function as well as its variables. The function

DEF fnhyp# (A,B) = SQR (A ^ 2 + B ^ 2)

calculates in double precision the hypotenuse of a right triangle with sides A and B (even if A and B are only single precision variables). The function

DEF fnfull$ (first$, last$) = first$ + " " + last$

combines two strings into one string with a separating space.

As you might suppose, the DEF fn statement defining a function must precede any statement utilizing this function. To ensure against any oversights, it is perhaps best to place all DEF fn statements near the beginning of your program.

DEF fn (Multiple Line Version)

The definition of a more complicated function may be spread over several lines. For instance, given a positive integer N the expression N! ("N factorial") is the product

N! = 1 · 2 · 3 · 4 · … · N .

But Basic will not understand the three dots, so we cannot use this formula for defining the factorial function in Basic. Instead we may use a multiple line DEF fn statement:

DEF fnfact(N)
P = 1
FOR I = 1 TO N
P = P * I
NEXT I
fnfact = P
END DEF

Observe that when N = 0 the FOR … NEXT loop is skipped, as there are no integers I in a run from 1 to 0 in steps of 1; in this case the definition returns 0! = 1, the initial value of P. The concluding END DEF is necessary to mark the end of the definition.

Factorials get large very fast. Setting N = 35 in the above definition produces an overflow error in Basic, as single precision handles numbers only as large as 3.4 x 1038. You can accommodate larger integers by specifying double precision. If you rename the function fnfact#, and further specify that P be double precision, then the definition produces no overflow until N reaches 171.

A much-used function in combinatorics is the permutation function,

P(N,M) = N · (N - 1) · (N - 2) · … · (N - M + 1) ,

where we begin with a nonnegative integer N and multiply downward one number at a time until we reach M factors. (M can be no larger than N, and when M = 0 we define P(N,0) = 1.) A multiple line definition of P(N,M) is

DEF fnperm (N, M)
P = 1
FOR I = N TO (N - M + 1) STEP -1
P = P * I
NEXT I
fnperm = P
END DEF

It is acceptable to define a function in terms of functions already defined. The Laguerre polynomial of degree n, a well known function in engineering and applied mathematics, is defined as
When n = 2 this formula gives
If we have already defined the functions fnfact and fnperm as above, then we can introduce to our Basic program the Laguerre polynomial Ln(x) as follows:

DEF fnlag(N,X)
sum = 0
FOR M = 0 TO N
T = fnperm(N,M) * (-X) ^ M
B = (fnfact(M)) ^ 2
sum = sum + T/B
NEXT M
fnlag = sum
END DEF

If we wanted double precision we would name the function fnlag# instead of fnlag.

INT (greatest integer function)

The Basic function INT (X) returns the greatest integer less than or equal to X.

Examples :

INT (2.317) = 2 , INT (5) = 5 , INT (-3.81) = - 4 .