0% found this document useful (0 votes)
30 views8 pages

CSC 112 - Lecture 6

Csc6

Uploaded by

Taiwo Subair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views8 pages

CSC 112 - Lecture 6

Csc6

Uploaded by

Taiwo Subair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

CSC 112 – LECTURE SIX

Functions and subroutines

Functions

In defining a function, DEF keyword is used. Functions are a set


or block of code statements which are executed together to
perform a particular task and return a value. This is necessary
when certain tasks have to be performed over and over again.
Rather than write the same set of codes over and over again for
each time the task is to be performed, the set of codes that
accomplish the task can be written to form a function and the
function can be called anytime the task is to be carried out.

e.g. 10 DEF FNA = 4

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.

Functions can be numeric or of string type. A numeric function is


one that returns a number after its execution and a string
function is one that returns a string as its result.

Numeric functions are defined without the dollar sign ($)while


string functions must have the dollar sign ($).

Example of string function - 10 DEF FNA$ = “Hello world”

Functions may have arguments passed to them for the operation


to be performed.

e.g. 10 DEF FNA(x) = x^2 +(3*x)

10 DEF FNA(x, y) = x^2 + y^2

The value of the expression on the right hand side is returned.


After the function is created, it has to be called from a point
within the program just as a library function is called.

When the function is being evaluated, the values of the


arguments must be specified by the point of reference within the
program and not the function definition. The arguments
appearing in the function in a DEF statement are called dummy
arguments since they are only a representative of the true
arguments to be actually passed in and used within the function.
The names of the arguments being used in the function definition
and those passed in by the calling point do not have to bear the
same name. In calling a function, the number of arguments being
passed to the function must be of the same number and type as
declared in the function.

A line of code for a function that computes the average of three


numbers is:

30 FNA (x, y, z) = (x+y+z)/3

Example

Write a program with functions that computes the sum and


average of five numbers.

Solution

10 REM This program does statistical operations with 5 numbers

20 DEF FNS (a, b, c, d, e) =a+b+c+d+e ’sum function

30 DEF FNA (a, b, c, d, e) = (a+b+c+d+e)/5 ’average


function

40 PRINT “Enter the 5 numbers”

50 INPUT f, g, h, i, j
60 PRINT “The sum of the five numbers is”; FNS (a, b, c, d, e)

70 PRINT “The average of the five numbers is”; FNA (a, b, c, d,


e)

80 END
Example

Write a program with a function that computes the square root of


the logarithm of xcosx.

Solution

10 REM This program computes the square root of the logarithm


of xcosx.

20 DEF FND (x) = SQR (LOG (X*COS (X)))

30 INPUT x

40 PRINT “The result is”; FND (x)

50 END

Example

Write a program with functions that computes the area and


volume of a sphere with radius, r.

Solution

10 REM This program computes the area and volume of a sphere

20 PRINT “Enter the radius of the sphere”

30 INPUT r

40 DEF FNA (r) = 4*3.142*r^2

50 DEF FNV (r) = 4/3*3.142*r^3

60 PRINT “The area is”; FNA (r)

70 PRINT “The volume is”; FNV (r)

80 END
Example

Write a program with a function that computes the absolute


value of the square root of the sine of a number and the square
root of the cosine of the number.

Solution

10 REM This program has two functions

20 DEF FNS (x) = SQR (SIN (X))

30 DEF FNC (x) = SQR (COS (X))

40 PRINT “Enter x”

50 INPUT x

60 PRINT “The square root of sine x is”; FNS (x)

70 PRINT “The square root of cos x is”; FNC (x)

80 END

Example

Write a program with a function that takes in three arguments;


one for the name of the user, the next for the age and the next
for the department.

Solution

10 REM This program displays a welcome message

20 PRINT “Enter your name, your age and your department”

30 INPUT name$, age, dept$

40 DEF FNB$ = “Welcome”


50 DEF FNC$ = “You are in the department of”

60 DEF FND$ = “Your age is”

70 PRINT FNB$; name$

80 PRINT FNC$; dept$

90 PRINT FND$; age

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.

There are multi-line functions which span more than a line.


There are so many calculations or operations which cannot be
carried out with a single line function where the operation is
complex and lengthy and also, if the use of conditional branching
is needed.

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.

e.g. 50 DEF FNA (X, Y)

60 LET D=X+Y

70 LET FNA=D ’value returned by the function

80 FNEND
Example

Write a simple program that has a function which determines the


smaller of a pair of numbers.

Solution

10 REM This program determines the smaller of 2 numbers

20 PRINT “Enter the two numbers”

30 DEF FNA (X, Y)

40 LET FNA=X

50 IF X<=Y THEN 70

60 LET FNA=Y

70 FNEND

80 INPUT A, B

90 PRINT “The smaller of the two numbers is”; FNA (A, B)

100 END

So far, it is seen that functions can only return one value and not
more.

Subroutines

Subroutines are similar to functions in that they are also a set of


codes which are executed when referred to from any point in a
program, but they are quite different from functions. A
subroutine does not have a name which is unlike functions and it
can return more than one numeric and/or string value.

A subroutine does not need to start with any special statement.


So, it can begin with any keyword, say a LET, REM, INPUT etc,
but the last statement must be a RETURN statement. With this,
control is transferred back to the calling point within the
program.

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

A subroutine can be referenced with the use of the GOSUB


statement. This includes the line number, the GOSUB keyword
and the line number of the first statement of the subroutine.

It is possible to have a reference from one subroutine to another


subroutine and in this way, the subroutines are said to be nested.

You might also like