A subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. They are often used to improve code readability, reuse, and maintainability, and can be invoked multiple times from various points of a program.
Subroutine in BASIC
In BASIC programming language, a subroutine is defined by the number of its first line and is executed when a GOSUB
(Go to Subroutine) command is encountered. The RETURN
statement marks the end of the subroutine, directing the flow of control back to the point immediately following the GOSUB
command.
Structure of a Subroutine
Typically, a subroutine in basic includes:
110 PRINT "Main Program"
220 GOSUB 1000
330 PRINT "After Subroutine"
440 END
51000 PRINT "Inside Subroutine"
61010 RETURN
In this example:
- Lines 10, 20, 30, and 40 are part of the main program.
- Line 20 calls the subroutine found at line 1000.
- Line 1010 marks the end of the subroutine with
RETURN
.
Types of Subroutines
- Procedural: Perform a specific task without returning a value.
- Function: Perform a task and return a value to the calling program.
Special Considerations
- Parameter Passing: Subroutines can accept parameters, which allows passing input data to them.
- Scope and Lifetime: Variables in subroutines can be local or global, affecting their lifetime and accessibility.
- Recursion: Subroutines can call themselves, called recursion, which requires careful handling to avoid infinite loops.
Examples and Applications
Mathematical Operations
Subroutines can be used to encapsulate complex mathematical operations, making the main program cleaner and more modular.
110 INPUT "Enter two numbers: ", A, B
220 GOSUB 1000 : REM CALL Subroutine to add numbers
330 PRINT "Sum: "; S
440 END
51000 S = A + B
61010 RETURN
File Handling
Subroutines help manage repeated file operations like opening, reading, and writing.
110 GOSUB 2000 : REM Open File
220 GOSUB 2100 : REM Read File
330 GOSUB 2200 : REM Close File
440 END
52000 OPEN "FILE.TXT" FOR INPUT AS #1
62010 RETURN
72100 INPUT #1, LINE
82110 PRINT LINE
92120 RETURN
102200 CLOSE #1
112210 RETURN
Historical Context
Subroutines date back to the early days of computing, providing a necessary structure to assembly and machine languages. In languages like Fortran and BASIC, subroutines were essential for managing complex programs on limited hardware.
Related Terms
- Procedure: A type of subroutine that does not return a value.
- Function: A subroutine that returns a value to the caller.
- GOSUB / RETURN: Specific commands in BASIC used to maneuver between the main program and its subroutines.
- Recursion: The process of a subroutine calling itself.
FAQs
What is the difference between a subroutine and a function?
Why use subroutines in programming?
Can subroutines be recursive?
How are subroutines used in modern programming languages?
References
- Kernighan, B. W., & Ritchie, D. M. (1988). The C Programming Language. Prentice Hall - Discusses subroutines in the C Programming language.
- Microsoft (n.d.). Subroutine in BASIC. Microsoft Developer Network - Documentation and examples for BASIC subroutines.
Summary
Subroutines are fundamental units in programming that encapsulate specific tasks to promote code efficiency and readability. Understanding subroutine structure, usage, and historical relevance helps in creating better-programmed modular and maintainable codebases.