As you explore QBASIC programming, you’ll find lots of ready-to-use functions that can make your coding easier and better. In this article, we’ll dive into important functions in QBASIC. We’ll give you examples and explanations to help you use them well.
What Are Built-in Functions?
Built-in functions, sometimes called built-in commands, are functions that are already included in the QBASIC programming language. These functions have specific jobs like doing math, changing text, or making choices. By using these functions, you can save time because the language already has them ready to use.
Mathematical Functions
Example 1: Abs()
The ABS()
function returns the absolute value of a number, effectively removing its sign.
DIM num AS INTEGER
num = -5
PRINT "Absolute value of "; num; " is "; ABS(num)
In this example, the ABS()
function ensures that the absolute value of -5
is displayed as 5
.
Example 2: SQR()
The SQR()
function calculates the square root of a given number.
DIM num AS INTEGER
num = 25
PRINT "Square root of "; num; " is "; SQR(num)
Here, the SQR()
function computes the square root of 25
, resulting in an output of 5
.
String Manipulation Functions
Example 3: LEN()
The LEN()
function determines the length of a string, counting the number of characters.
DIM text AS STRING
text = "Hello, QBASIC"
PRINT "Length of text: "; LEN(text)
In this code, the LEN()
function calculates the length of the string "Hello, QBASIC"
, yielding a length of 12
.
Example 4: LEFT$() and RIGHT$()
The LEFT$()
and RIGHT$()
functions extract a specified number of characters from the left or right side of a string, respectively.
DIM message AS STRING
message = "QBASIC is amazing!"
PRINT "Left part: "; LEFT$(message, 6)
PRINT "Right part: "; RIGHT$(message, 8)
These functions split the string "QBASIC is amazing!"
, providing the leftmost 6
characters (“QBASIC”) and the rightmost 8
characters (“amazing!”).
Decision-Making Functions
Example 5: IIF()
The IIF()
function, also known as the Immediate If function, allows you to create conditional expressions with concise syntax.
DIM num AS INTEGER
num = 10
PRINT "Result: "; IIF(num > 5, "Greater", "Less or equal")
In this example, the IIF()
function checks if num
is greater than 5
. Depending on the condition, it prints either “Greater” or “Less or equal”.
Conclusion
Built-in functions are like valuable tools in your QBASIC programming toolbox. They help you do hard things easily, change text without trouble, and make smart choices depending on situations. When you use these functions in your code, you become a more efficient and productive programmer. As you keep learning QBASIC, feel free to check out all the different built-in functions that you can use.