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 efficient. These functions are like "ready-made" programs that accept data, process it, and return a result, avoiding the need to write the same code repeatedly.
- A function is a structure that simplifies a complex operation into a single step, like a "black box".
- These are predefined functions in BASIC that perform various operations.
- Functions often require input values, called arguments, to perform their operations.
- After processing the input, a function returns a value, which can be used in other parts of the program.
- Built-in functions reduce the need to write complex code, saving time and effort.
- Using built-in functions makes code easier to understand and maintain.
- Predefined functions are less prone to errors compared to user-defined code.
- 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:
2. INT (Integer Part)
-
Syntax:
INT(X) -
Description: Returns the greatest integer less than or equal to
X. -
Example:
3. CINT (Convert to Integer)
-
Syntax:
CINT(X) -
Description: Rounds
Xto the nearest whole number. -
Example:
4. FIX (Truncate)
-
Syntax:
FIX(X) -
Description: Truncates
Xto the integer part (removes decimal). -
Example:
5. ABS (Absolute Value)
-
Syntax:
ABS(X) -
Description: Returns the positive value of
X, ignoring its sign. -
Example:
6. RND (Random Number)
-
Syntax:
RND -
Description: Generates a random number between 0 and 1.
-
Example:
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
XbyY. -
Example:
9. SGN (Sign)
-
Syntax:
SGN(X) -
Description: Returns the sign of a number:
-
1if X > 0 -
0if X = 0 -
-1if X < 0
-
-
Example:
10. EXP (Exponent)
-
Syntax:
EXP(X) -
Description: Returns e^X, where e ≈ 2.71828.
-
Example:
11. LOG (Natural Logarithm)
-
Syntax:
LOG(X) -
Description: Returns the natural logarithm of a positive number X.
-
Example:
🔤 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
2. Find the Sine of a Value
3. Plot a Cosine Graph
🎯 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
Post a Comment