BASIC Programming II (Built-in Functions)

TOPIC: BASIC Programming II (Built-in Functions)

CLASS: SSS Two

What is a Function?

A function is a pre-written set of instructions that performs a specific task and often returns a result. It allows you to perform complex operations by simply calling its name, making your programs shorter and easier to understand.

Definition of BASIC Built-in Functions

BASIC built-in functions are special, predefined functions that are already integrated into the BASIC interpreter. They can be used directly in your programs to perform a wide range of common operations without you having to write the code for them yourself.

Some BASIC Built-in Functions

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

1. CHR$ function

The CHR$ function returns a string (text character) corresponding to a specified ASCII (American Standard Code for Information Interchange) value. The syntax is `CHR$(X)`, where "X" is a whole number (decimal ASCII code) between 0 and 255.

Example:


PRINT CHR$(65)
PRINT CHR$(66)

Output:


A
B

2. SQR Function

The SQR function calculates the square root of a non-negative number. The general form of the function is `SQR(X)`, where X must be greater than or equal to 0.

Example:


PRINT SQR(9)
PRINT SQR(2)

Output:


3
1.414214

3. INT Function

The INT function finds the greatest integer that is less than or equal to a number. It effectively truncates (chops off) the decimal part for positive numbers, but for negative numbers, it rounds down to the next whole number.

The general form of the function is `INT(X)`.

Examples:


PRINT INT(15.46)
PRINT INT(-15.46)
PRINT INT(15.56)
PRINT INT(-15.56)

Output:


15
-16
15
-16

4. CINT Function

The CINT function (Integer Conversion) is used to convert a number into an integer by rounding it to the nearest whole number. For numbers ending in .5, it typically rounds to the nearest even integer (known as "banker's rounding").

Examples:


PRINT CINT(15.46)
PRINT CINT(-15.46)
PRINT CINT(15.56)
PRINT CINT(-15.56)
PRINT CINT(14.5)
PRINT CINT(15.5)

Output:


15
-15
16
-16
14
16

5. FIX Function

The FIX function truncates a number into an integer by simply removing the decimal part, moving the number towards zero. It does not round.

The general form of the function is `FIX(X)`.

Examples:


PRINT FIX(15.46)
PRINT FIX(-15.46)
PRINT FIX(15.56)
PRINT FIX(-15.56)

Output:


15
-15
15
-15

Comparison of INT, CINT, and FIX:

Input (X) INT(X) CINT(X) FIX(X)
15.46 15 15 15
15.56 15 16 15
-15.46 -16 -15 -15
-15.56 -16 -16 -15

6. ABS Function

The ABS function (Absolute value) returns the absolute value of a number, meaning the number without any sign (it's always positive). The general form of the function is `ABS(X)`.

Examples:


PRINT ABS(+3.4)
PRINT ABS(-3.4)
PRINT ABS(0)

Output:


3.4
3.4
0

7. RND Function

The RND function (Random) generates a pseudo-random number between 0 (inclusive) and 1 (exclusive). By default, BASIC's RND function generates the same sequence of "random" numbers each time the program is run unless `RANDOMIZE TIMER` is used at the beginning of the program.

Example:


PRINT RND
PRINT RND

Output:
(You will get two numbers that appear random, e.g.,)


0.234567
0.876543

If you run the program again, you will get the same two numbers unless `RANDOMIZE TIMER` is included:


RANDOMIZE TIMER
PRINT RND
PRINT RND

This program will print different random numbers each time it runs.

8. COS, SIN, TAN, and ATN Functions

These are trigonometric functions that operate on angles measured in radians. (Remember that `1 radian` is approximately `57.3 degrees`).

  • COS(X): Returns the cosine of X.
  • SIN(X): Returns the sine of X.
  • TAN(X): Returns the tangent of X.
  • ATN(X): Returns the arctangent of X (the angle whose tangent is X).

The general form for these functions is `COS(X)`, `SIN(X)`, `TAN(X)`, and `ATN(X)` respectively.

9. MOD Operator (Remainder)

The MOD operator returns the remainder of a division operation. Its general form is `X MOD Y`, where X is the dividend and Y is the divisor.

Examples:


PRINT 16 MOD 5
PRINT 30 MOD 5

Output:


1
0

10. SGN Function

The SGN function (Sign) returns the sign of the input number as a numeric value:

  • Returns `1` if the number is positive.
  • Returns `-1` if the number is negative.
  • Returns `0` if the number is zero.

The general form of the function is `SGN(X)`.

Examples:


PRINT SGN(54)
PRINT SGN(-54)
PRINT SGN(0)

Output:


1
-1
0

11. EXP Function

The EXP function returns the natural exponent of X (e raised to the power of X), where `e` is the base of the natural logarithm, approximately `2.718281828`. The general form of the function is `EXP(X)`.

Examples:


PRINT EXP(4)
PRINT EXP(-5)

Output:


54.59815
6.737947E-03 ' (This is scientific notation for 0.006737947)

12. LOG Function

The LOG function returns the natural logarithm of a numeric expression (the logarithm to base `e`). The numeric expression (X) must be positive. The syntax is `LOG(X)`.

BASIC Notation for Mathematical Expressions

In BASIC programming language, every arithmetic expression must appear on a single line. There is no superscript or special formatting like we find in algebra; instead, operators like `^` (for exponentiation), `*` (for multiplication), and `/` (for division) are used, along with parentheses to control the order of operations.

Examples:

Mathematics Expression BASIC Expression
x = (-b ± √(b² - 4ac)) / (2a) X = (-B + SQR(B^2 - 4 * A * C)) / (2 * A)
X = (-B - SQR(B^2 - 4 * A * C)) / (2 * A)
(x - y) / (x + y) (X - Y) / (X + Y)
e^(x² + y) - Sin(x + ny) EXP(X^2 + Y) - SIN(X + N * Y)
b = 1 / (4ac) B = 1 / (4 * A * C)
(2x² - 3x - 1) / (x² - x - 6) (2 * X^2 - 3 * X - 1) / (X^2 - X - 6)

Some BASIC Programs Using Built-in Functions

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 in a range
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 "; I; " is "; SQR(I)
60 NEXT I
70 END

[run] Example Interaction:


Enter the first number of range: ? 1
Enter the last number of range: ? 5
The square root of 1 is 1
The square root of 2 is 1.414214
The square root of 3 is 1.732051
The square root of 4 is 2
The square root of 5 is 2.236068

Example 2: Write a program to find the Sine of an unknown value

Solution


10 REM Program to find the Sine of unknown value
20 INPUT "Enter the number (angle in radians): "; A
30 LET S = SIN(A)
40 PRINT "The Sine of "; A; " is "; S
50 END

[run] Example Interaction:


Enter the number (angle in radians): ? 1.5708 ' (approx. pi/2 radians)
The Sine of 1.5708 is 1

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

[run] Output:


ABCDEFGHIJKLMNOPQRSTUVWXYZ

Example 4: Write a program to plot a 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

[run] Output:
This program will display a simple cosine wave graph on the screen in a graphics mode window.

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
  11. I LOVE CMPNOTES

    ReplyDelete
  12. In BASIC programming as you have taught, is r**2 the same as r^2 when denoting the square of r?

    ReplyDelete
    Replies
    1. No, r**2 is not the same as r^2. The asterisk (*) sign is multiplication sign. Where as the carat sign (^) is raise to power

      Delete
  13. You are really doing a nice job! Your content is helpful to teach in a clear and concise form. Thanks cmpnote.

    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 3 Objective Questions and Answers in Computer studies