CSC 180.1 · Introduction to Computer Science and BASIC Programming
Our notes
1 min readWhy subroutines
Large problems are easier when split into smaller parts (modular or top-down design). A subroutine is a block of code that does one job and can be called from many places.
Benefits: no repeated code, easier testing and debugging, team work, clearer programs.
GOSUB and RETURN (classic BASIC)
GOSUB n jumps to line n and remembers where it came from; RETURN goes back to the line after the GOSUB.
10 INPUT "RADIUS"; R
20 GOSUB 100
30 PRINT "AREA ="; A
40 INPUT "ANOTHER RADIUS"; R
50 GOSUB 100
60 PRINT "AREA ="; A
70 END
100 REM SUBROUTINE: AREA OF A CIRCLE
110 LET A = 3.14159 * R^2
120 RETURN
Radii 1 and 2 give areas about 3.14 and 12.57.
The END at line 70 stops the program from "falling into" the subroutine.
ON...GOSUB (menus)
ON K GOSUB 200, 300, 400 calls the first, second or third subroutine when K is 1, 2 or 3; useful for menus.
Subprograms in structured BASIC (QBasic)
DECLARE SUB Greet (N$)
CALL Greet("ADA")
END
SUB Greet (N$)
PRINT "WELCOME, "; N$
END SUB
Prints WELCOME, ADA. A SUB has its own local variables and receives values through parameters, so it does not clash with the main program. FUNCTION procedures return a value.
GOSUB against SUB
| GOSUB subroutine | SUB procedure |
|---|---|
| uses line numbers | uses a name |
| shares all variables with the main program | local variables, parameters |
| easy to misuse | safer, structured |
Top-down design example: a result program
- Main program: read student data, call subroutines, print.
- Subroutine 1: compute total and average.
- Subroutine 2: find the grade.
- Subroutine 3: print a report line.
Exams usually ask
- Explain GOSUB and RETURN with an example program.
- State the advantages of subroutines and modular programming.
- Distinguish a subroutine from a user-defined function.
Common mistake
Leaving out END (or STOP) before the subroutine lines, so the program runs into the subroutine and then hits RETURN without GOSUB.
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
-
Structured programming
(opens en.wikipedia.org in a new tab)
Wikipedia, Structured programming, CC BY-SA 4.0 · licence
-
Goto
(opens en.wikipedia.org in a new tab)
Wikipedia, Goto, 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
-
QBASIC TUTORIAL: FUNCTION and SUB Concepts
16 min · ≈ 72 MB · about ₦40
(opens youtube.com in a new tab)
-
QBasic Tutorial 36 - Making A Sub In QB64
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
Sub Routine | GoSub Return Statement | GW BASIC Programming Tutorials
17 min · ≈ 77 MB · about ₦43
(opens youtube.com in a new tab)
-
BASIC Programming lesson 14 - Gosub Return
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
PlayBASIC Tutorial: Intro to GOTO & GOSUB statements (Beginners)
36 min · ≈ 162 MB · about ₦91
(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.