CSC 180.1 · Introduction to Computer Science and BASIC Programming
Our notes
1 min readLibrary (built-in) functions
| Function | Meaning | Example |
|---|---|---|
| SQR(X) | square root | SQR(49) = 7 |
| ABS(X) | absolute value | ABS(−6) = 6 |
| INT(X) | largest whole number ≤ X | INT(4.8) = 4; INT(−4.8) = −5 |
| FIX(X) | drops the fraction (QBasic) | FIX(−4.8) = −4 |
| SGN(X) | sign: −1, 0 or 1 | SGN(−3) = −1 |
| SIN, COS, TAN, ATN | trigonometry (radians) | SIN(0) = 0 |
| LOG(X), EXP(X) | natural log, e^x | LOG(1) = 0 |
| RND | random number between 0 and 1 | INT(RND*6)+1 gives a die roll 1-6 |
| LEN(S$) | length of a string | LEN("NAIRA") = 5 |
| LEFT$, RIGHT$, MID$ | parts of a string | LEFT$("UNIPORT", 3) = "UNI" |
| STR$, VAL | number to string and back | VAL("12") = 12 |
Angles in degrees must be converted: radians = degrees × π / 180.
Rounding with INT
To round X to the nearest whole number: INT(X + 0.5). INT(3.6 + 0.5) = INT(4.1) = 4.
To 2 decimal places: INT(X * 100 + 0.5) / 100: 3.14159 → 3.14.
Worked example: roots of a quadratic
10 INPUT A, B, C
20 LET D = B^2 - 4*A*C
30 IF D < 0 THEN PRINT "NO REAL ROOTS" : GOTO 70
40 LET X1 = (-B + SQR(D)) / (2*A)
50 LET X2 = (-B - SQR(D)) / (2*A)
60 PRINT X1, X2
70 END
For x² − 5x + 6 = 0 (A = 1, B = −5, C = 6): D = 25 − 24 = 1; roots 3 and 2 (check: 3 + 2 = 5, 3 × 2 = 6 ✓).
User-defined functions: DEF FN
A one-line formula given a name and used many times.
10 DEF FNA(R) = 3.14159 * R^2
20 PRINT FNA(7)
30 END
Prints about 153.94 (3.14159 × 49).
Rules: the name starts with FN; the function is defined once before use; the variable in brackets is a dummy argument. QBasic also allows multi-line FUNCTION... END FUNCTION.
Why functions help
They save rewriting formulas, reduce errors, and make programs easier to read.
Exams usually ask
- Give the value of built-in functions (INT, ABS, SQR, LEN, MID$).
- Write a program using a library function (quadratic roots, distance, rounding).
- Define and use a DEF FN function.
Common mistake
Thinking INT rounds. INT(4.9) is 4; for negative numbers INT(−4.2) is −5, because it goes down to the next whole number.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Function (computer programming)
(opens en.wikipedia.org in a new tab)
Wikipedia, Function (computer programming), CC BY-SA 4.0 · licence
-
Library (computing)
(opens en.wikipedia.org in a new tab)
Wikipedia, Library (computing), CC BY-SA 4.0 · licence
-
NOUN CIT 132: Programming in BASIC
NOUN course book (PDF, about 0.8 MB). The whole BASIC programming course.
(opens nou.edu.ng in a new tab)
Watch
-
QB64 Basic programming tutorial
10 min · ≈ 45 MB · about ₦25
(opens youtube.com in a new tab)
-
QBasic Tutorial 38 - Making A Function - QB64
7 min · ≈ 32 MB · about ₦18
(opens youtube.com in a new tab)
-
QBASIC TUTORIAL: FUNCTION and SUB Concepts
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
QB64 Tutorial 7 - Simple Calculator
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
Function Procedure in QBASIC
1 h · ≈ 270 MB · about ₦152
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.