PROGRAMMING IN BASIC II - Built-in Functions

Introduction 

In the context of BASIC programming, built-in functions are predefined procedures that perform specific tasks, simplifying programming and making it more efficientThese functions are like "ready-made" programs that accept data, process it, and return a result, avoiding the need to write the same code repeatedly. 

Key Concepts:
  • Function Definition:
    A function is a structure that simplifies a complex operation into a single step, like a "black box".
  • Built-in Functions:
    These are predefined functions in BASIC that perform various operations.
  • Function Arguments:
    Functions often require input values, called arguments, to perform their operations.
  • Return Value:
    After processing the input, a function returns a value, which can be used in other parts of the program. 
  Benefits of Using Built-in Functions:
  • Simplified Programming:
    Built-in functions reduce the need to write complex code, saving time and effort.
  • Improved Readability:
    Using built-in functions makes code easier to understand and maintain.
  • Error Reduction:
    Predefined functions are less prone to errors compared to user-defined code.
  • Increased Efficiency:
    Built-in functions are often optimized for performance, leading to faster execution.
  • Examples of Built-in Functions:

  • 1. SQR (Square Root)
    • Syntax: SQR(X)

    • Description: Returns the square root of a number X.

    • Example:

      PRINT SQR(9) 'Output: 3
      PRINT SQR(2) 'Output: 1.414214

    2. INT (Integer Part)

    • Syntax: INT(X)

    • Description: Returns the greatest integer less than or equal to X.

    • Example:

      PRINT INT(15.46) 'Output: 15 PRINT INT(-15.46) 'Output: -16

    3. CINT (Convert to Integer)

    • Syntax: CINT(X)

    • Description: Rounds X to the nearest whole number.

    • Example:

      PRINT CINT(15.56) 'Output: 16
      PRINT CINT(-15.56) 'Output: -16

    4. FIX (Truncate)

    • Syntax: FIX(X)

    • Description: Truncates X to the integer part (removes decimal).

    • Example:

      PRINT FIX(15.46) 'Output: 15
      PRINT FIX(-15.46) 'Output: -15

    5. ABS (Absolute Value)

    • Syntax: ABS(X)

    • Description: Returns the positive value of X, ignoring its sign.

    • Example:

      PRINT ABS(-3.4) 'Output: 3.4
      PRINT ABS(3.4) 'Output: 3.4

    6. RND (Random Number)

    • Syntax: RND

    • Description: Generates a random number between 0 and 1.

    • Example:

      PRINT RND PRINT RND

    7. Trigonometric Functions

    • Syntax:

      • SIN(X) – Returns sine of X

      • COS(X) – Returns cosine of X

      • TAN(X) – Returns tangent of X

      • ATN(X) – Returns arctangent of X

    • Note: X is in radians.

    8. MOD (Modulus/Remainder)

    • Syntax: X MOD Y

    • Description: Returns the remainder after dividing X by Y.

    • Example:

      PRINT 16 MOD 5 'Output: 1 PRINT 30 MOD 5 'Output: 0

    9. SGN (Sign)

    • Syntax: SGN(X)

    • Description: Returns the sign of a number:

      • 1 if X > 0

      • 0 if X = 0

      • -1 if X < 0

    • Example:

      PRINT SGN(54) 'Output: 1 PRINT SGN(-54) 'Output: -1 PRINT SGN(0) 'Output: 0

    10. EXP (Exponent)

    • Syntax: EXP(X)

    • Description: Returns e^X, where e ≈ 2.71828.

    • Example:

      PRINT EXP(4) 'Output: 54.59815 PRINT EXP(-5) 'Output: 0.006737947

    11. LOG (Natural Logarithm)

    • Syntax: LOG(X)

    • Description: Returns the natural logarithm of a positive number X.

    • Example:

      PRINT LOG(10) 'Output: 2.302585

    🔤 Important Note on BASIC Syntax

    • All arithmetic expressions must be written on a single line.

    • BASIC does not use superscripts for exponents like in algebra.


    🧮 Sample BASIC Programs

     1. Find Square Roots of Numbers in a Range

    10 REM Find square roots in a range 20 INPUT "Enter the first number of the range"; A 30 INPUT "Enter the last number of the range"; B 40 FOR I = A TO B 50 PRINT "The square root of"; I; "is"; SQR(I) 60 NEXT I 70 END

     2. Find the Sine of a Value

    10 REM Find sine of a number
    20 INPUT "Enter the number"; A 30 LET S = SIN(A) 40 PRINT "The Sine of"; A; "is"; S 50 END

    3. Plot a Cosine Graph

    10 REM Plotting a 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

    🎯 Summary

    BASIC’s built-in functions make it easier to perform mathematical operations without writing complex code. Whether you're finding square roots, rounding numbers, generating random values, or using trigonometric calculations—BASIC functions help you do it efficiently.

Comments

Popular posts from this blog

The Internet JSS2

FILE SHARING -JS2