BASIC Programming II (Built-in Functions)

TOPIC: BASIC Programming II (Built-in Functions)

CLASS: SSS Two

What is a Function

A function is a structure that simplifies a complex operation into a single-step

Definition of BASIC Built-in Functions

BASIC built-in functions are predefined functions integrated into BASIC interpreter, that can be used to perform a wide range of operations.

Some BASIC Built-in Functions

BASIC has several built-in functions that greatly extend its capability. They include the following:

The CHR$ function returns a string containing the character associated with the specified ASCII (American Standard Code for Information Interchange). The syntax is CHR$(X). "X" is a whole number in decimal number system.
Example
PRINT CHR$ (65) returns letter A
PRINT CHR$ (66) returns letter B


The SQR function calculates the square root of a number. The general form of the function is SQR(X)
Example
PRINT SQR(9) will return 3
PRINT SQR(2) will return 1.414214


The INT function finds the greatest integer less than or equal to a number. The general form of the function is INT(X)
Example
PRINT INT(15.46) will return 15
PRINT INT(-15.46) will return -16
PRINT INT(15.56) will return 15
PRINT INT(-15.56) will return -16


CINT means Integer Conversion. This function is used to convert a number into an integer. It rounds off the number to the nearest integer value.
Example
PRINT CINT(15.46) will return 15
PRINT CINT(-15.46) will return -15
PRINT CINT(15.56) will return 16
PRINT CINT(-15.56) will return 16


This function truncates the number into an integer. The General form of the function is FIX (X)
Example
PRINT FIX(15.46) will return 15
PRINT FIX(-15.46) will return -15
PRINT FIX(15.56) will return 15
PRINT FIX(-15.56) will return -15


ABS means absolute. It is used to find the absolute value of a number. The absolute value of a number means the number without any sign. The general form of the function is ABS(X)
Example
PRINT ABS(+3.4) returns 3.4
PRINT ABS(-3.4) returns 3.4


RND means random. RND is a special function that gives us a random number between 0 and 1
Example
PRINT RND
PRIND RND
This program will print RND twice. Notice that you’ll get to numbers that appear to be unpredictable and random. But, try running the program again. You’ll get the same random numbers.


The COS, SIN, TAN, and ATN trigonometric functions are used to find the Cosine, Sine, Tangent and Arctangent of a particular numeric expression. The general form is:
COS(X)
SIN(X)
TAN(X)
ATN(X)


It means remainder. This function returns the remainder. The general form of the function is X MOD Y
Example:
PRINT 16 MOD 5 will return 1
PRINT 30 MOD 5 will return 0


It means sign. This returns the sign of the input number in numeric value. The general form of the function is SGN(X).
Examples
PRINT SGN(54) will return 1
PRINT SGN(-54) will return -1
PRINT SGN(0) will return 0


It is used to find the natural exponent of x, where e = 2.718281828. the general form of the function is EXP(X)
Example
EXP(4) will return 54.59815
EXP(-5) will return 6.737947E-03

LOG Function
This function returns the natural logarithm of a numeric expression (any positive numeric expression). The syntax is LOG(X)

BASIC Notation

In a BASIC programming language, every arithmetic expression must appear on a single line. There is no superscript in BASIC as we find in algebra.
Examples
Mathematics ExpressionBASIC Expression
x=-b±b2-4ac2a X=(-B±SQR(B^2-4*A*C))/(2*A)
x-yx+y (X-Y)/(X+Y)
ex2+y-Sin(x+ny) EXP(X^2+Y)-SIN(X+N*Y)
b=14ac B=1/(4*A*C)
2x2-3x-1x2-x-6 (2*X^2-3*X-1)/(X^2-X-6)

Some BASIC Programs

Example 1: Write a BASIC program to find the square root of numbers in a given range
Solution
10 REM program to find the square root of numbers
20 INPUT “Enter the first number of range”; A
30 INPUT “ENTER the last number of range”; B
40 FOR I = A TO B
50 PRINT “the square root of “; A; “is”; SQR(A)
60 NEXT I
70 END

Example 2: Write a program to find the Sine of unknown values
Solution
10 REM Program to find the Sine of unknown value
20 INPUT “Enter the number”; A
30 LET S = SIN(A)
40 PRINT “The Sine of”; A; “is”; S
50 END <

Example 3: Write a program to output letters A - Z
Solution
10 REM this program is written to display letters from A to Z
20 FOR I = 65 TO 90
30 PRINT CHR$(I);
40 NEXT I
50 END

Example 4: Write a program to plot Cosine Graph
Solution
10 REM Program to plot cosine graph
20 SCREEN 13
30 FOR X% = 0 TO 360
40 PSET (X%, (COS(X% * 0.017453) * 50) + 50), 15
50 NEXT X%
60 END

Comments

  1. I do appreciate all you do for us all...

    ReplyDelete
  2. Thank you very much it really helped me

    ReplyDelete
  3. Thank you so much

    ReplyDelete
  4. Than you so much for helping me

    ReplyDelete
  5. Youve really helped me in this subject. God bless you for this. Thank you very much

    ReplyDelete
  6. String functions

    ReplyDelete
  7. Thank you very much. Because of you I could teach my students very well

    ReplyDelete
    Replies
    1. Thanks for your kind words. You are always welcome

      Delete
  8. Good work. Keep up. Thank you very much.

    ReplyDelete
  9. What is the conversion of area to basic expression

    ReplyDelete
  10. Please, 🙏 what's the conversion formula for AREA to Basic expression ?

    ReplyDelete

Post a Comment

Popular posts from this blog

90 Objective Examination Questions in Major Subjects

Complete Computer Studies/ICT Curriculum for JSS 1 to SSS 3

JSS One Objective Questions in Computer Studies/Information Technology