UNIHIVE

Study Hub UNIPORT CSC 180.1

CSC 180.1 · Introduction to Computer Science and BASIC Programming

Library and user-defined functions

Built-in numeric and string functions, random numbers, and defining your own functions with DEF FN.

Our notes

1 min read

Library (built-in) functions

FunctionMeaningExample
SQR(X)square rootSQR(49) = 7
ABS(X)absolute valueABS(−6) = 6
INT(X)largest whole number ≤ XINT(4.8) = 4; INT(−4.8) = −5
FIX(X)drops the fraction (QBasic)FIX(−4.8) = −4
SGN(X)sign: −1, 0 or 1SGN(−3) = −1
SIN, COS, TAN, ATNtrigonometry (radians)SIN(0) = 0
LOG(X), EXP(X)natural log, e^xLOG(1) = 0
RNDrandom number between 0 and 1INT(RND*6)+1 gives a die roll 1-6
LEN(S$)length of a stringLEN("NAIRA") = 5
LEFT$, RIGHT$, MID$parts of a stringLEFT$("UNIPORT", 3) = "UNI"
STR$, VALnumber to string and backVAL("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

Watch

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.

All CSC 180.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.