BASIC PROGRAMMING II
BUILT-IN FUNCTIONS
A function is an operation stored in or built in BASIC programming language, which when
issued performs a particular task.
A function may associate with one or more values as inputs and returns only a single value
as output.
BASIC has a number of built-in functions which greatly extend its capability.
1. SQR: the SQR function calculates the square root of a number. E.g.: SQR(9) is 3,
SQR(16) is 4.
Example:
10 REM CALCULATE SQUARE ROOT OF A NUMBER
20 LET N=100
30 PRINT SQR(N)
40 END
Example: Write a program to find the square root of 3a+2b, if a=2 and b=3.
10 REM CALCULATE THE SQUARE ROOT OF 3A+2B
20 READ A, B
30 DATA 2, 3
40 PRINT SQR(3*A+2*B)
50 END
2. INT: the function INT finds the greatest integer less than or equal to a number. It
discards the decimal part of positive numbers. E.g.: INT(2.7) is 2.
Example:
10 REM EVALUATING FUNCTIONS
20 LET N=6.25
30 PRINT INT(N)
40 END
3. ABS: the ABS function is used to find the absolute value of a number. Absolute value
of a number means the value itself without any sign.
Example:
10 REM FINDING ABSOLUTE VALUE
20 PRINT ABS(-2.8)
30 PRINT ABS(+3.5)
40 END
4. RND: the RND function is a function that gives a random number between 0 and 1.
Example:
10 CLS
20 PRINT RND
30 PRINT RND
5. COS, SIN, TAN, ATN: these functions will find the cosine, sine, tangent and
arctangent (inverse of tangent) of a value.
Example:
CONST PI=3.141593
PRINT COS(PI/4)
PRINT SIN(PI/3)
PRINT TAN(-PI/2)
PRINT ATN(TAN(-PI/2))
6. LOG: it finds the natural logarithm of a number.
Example:
PRINT LOG(28)
PRINT LOG(136)
7. EXP: it is used to calculate the exponential function (raising e to the power of any
value), where the value of e is 2.13
Example:
PRINT EXP(4)
PRINT EXP(-7)
8. MOD: MOD is means remainder. It is a function used to find the remainder when a
number is divided by another.
Example:
PRINT 15 MOD 4
PRINT 32 MOD 7
BASIC NOTATIONS
2
1. x= √
b −4 ac
2a
=SQR(B^2- 4*A*C)/2*A
x− y
2. x+ y
=(X-Y)/(X+Y)
3. ( a+ b ) +c ÷sin d
=(A+B) + C/SIN D
4. ex2+y – Sin (x +ny)
=EXP(X^2+Y) - (SIN(X+N*Y)
5. b=1/4ac
B=1/4*A*C
SIMPLE BASIC PROGRAMS USING FUNCTIONS
1. To find the square root of numbers within a given range.
10 REM PROGRAM TO FIND SQUARE ROOTS
20 INPUT “ENTER FIRST NUMBER OF RANGE”; A
30 INPUT “ENTER LAST NUMBER OF RANGE”; B
40 FOR I=A TO B
50 PRINT “THE SQUARE ROOT OF”; A; “IS”; SQR(A)
60 NEXT A
70 END
2. To find square root, S, rounded up to an integer.
10 REM FIND SQUARE ROOT
20 INPUT “ENTER NUMBER”; A
30 S=INT(SQR(A))
40 PRINT “THE SQUARE ROOT S ROUND OFF OF”; A; “IS”, S
50 END
3. To find the cosine of known values.
10 REM FIND COSINE OF VALUES
20 INPUT “ENTER NUMBER”; A
30 S=SIN(A)
40 PRINT “THE SINE OF”; A; “IS”; S
50 END
4. To find the tangent of a given value.
10 REM FIND TANGENT OF A VALUE
20 INPUT “ENTER NUMBER”; A
30 S=TAN(A)
40 PRINT “THE TANGENT OF”; A; “IS”; S
50 END
ASSIGNMENT
1. Write a program in BASIC to calculate the average score of students that offered six
subjects in an examination using READ and DATA statement.
2. Using INPUT statement, calculate the wages of a company’s employees (W= No of
hours X Rate).
3. Write a program in BASIC to calculate the discount that accrues to a customer for
buying items from a supermarket, using READ and DATA statement.
4. Write a program in BASIC to output (print) the Names, Age and Sex (Surname first)
of 5 students using READ and DATA statement.
5. Write a program to print the school name, Address and State of ten schools, using
READ and DATA statement.