CSC 112 - Lecture 6
CSC 112 - Lecture 6
Functions
In writing functions, the line number comes first, then the DEF
keyword and then the function name. A function name must be
made up of only 3 letters of which two must be FN signifying a
function definition. In the example above, a function FNA is
defined.
Example
Solution
50 INPUT f, g, h, i, j
60 PRINT “The sum of the five numbers is”; FNS (a, b, c, d, e)
80 END
Example
Solution
30 INPUT x
50 END
Example
Solution
30 INPUT r
80 END
Example
Solution
40 PRINT “Enter x”
50 INPUT x
80 END
Example
Solution
100 END
So far, the kind of function that has been considered is called the
single-line function. It is so-called because the function definition
spans only one line.
With the multi-line function, the line number comes first, then
the DEF keyword and then, the name of the function with
arguments in parenthesis, if required. The rest of the codes in
the function can then span more lines but at the end of the
function, the FNEND keyword must be used.
60 LET D=X+Y
80 FNEND
Example
Solution
40 LET FNA=X
50 IF X<=Y THEN 70
60 LET FNA=Y
70 FNEND
80 INPUT A, B
100 END
So far, it is seen that functions can only return one value and not
more.
Subroutines
You cannot branch out of a subroutine with the use of the IF-
GOTO statements. However, the variables used in a subroutine
must have been assigned values before the subroutine can be
called. The subroutine can also have many return statements to
return control to the calling point in the program.
Referencing a subroutine