Definition
A subroutine, sometimes referred to as a procedure, routine, or method, is a segment of code that is executed as a unit by a main program. It is designed to perform a specific task and can be called upon multiple times within a single program to avoid code duplication and facilitate code reuse and maintenance.
Examples
-
BASIC Programming Language Example:
110 PRINT "This is the main program."
220 GOSUB 100
330 PRINT "Returned from subroutine."
440 END
5
6100 PRINT "This is the subroutine."
7110 RETURN
-
Python Example:
1def greet():
2 print("Hello, World!")
3
4print("Main program starts.")
5greet()
6print("Main program ends.")
-
C++ Example:
1#include <iostream>
2using namespace std;
3
4void greet() {
5 cout << "Hello, World!" << endl;
6}
7
8int main() {
9 cout << "Main program starts." << endl;
10 greet();
11 cout << "Main program ends." << endl;
12 return 0;
13}
Frequently Asked Questions (FAQs)
Q1: What is the purpose of having subroutines in a program?
A1: Subroutines help in breaking down complex programs into smaller, manageable, and reusable components. They facilitate code reuse, reduce redundancy, and improve overall code maintainability and readability.
Q2: How does a subroutine differ from a function?
A2: While both subroutines and functions are similar in that they encapsulate code for reuse, functions typically return a value and subroutines may not. In some programming languages, these terms are used interchangeably.
Q3: How do you call a subroutine in BASIC?
A3: In BASIC, a subroutine is called using the GOSUB
statement, followed by the line number where the subroutine starts. The RETURN
statement is used to return control back to the main program.
- Function: A block of code designed to perform a specific task, usually with input parameters and a return value.
- Method: A function associated with an object, common in object-oriented programming.
- Procedure: Another term for a subroutine, used interchangeably in some programming languages.
Online Resources
Suggested Books for Further Studies
- “Code Complete: A Practical Handbook of Software Construction” by Steve McConnell
- “Clean Code: A Handbook of Agile Software Craftsmanship” by Robert C. Martin
- “Programming Pearls” by Jon Bentley
Fundamentals of Subroutine: Computer Programming Basics Quiz
### What is the primary advantage of using subroutines in a program?
- [x] Code reuse and maintainability
- [ ] Increase in code redundancy
- [ ] Decrease academic complexity
- [ ] None of the above
> **Explanation:** Subroutines improve code reuse and maintainability by allowing repetitive tasks to be placed in a single block of code that can be called multiple times.
### In BASIC, which command is used to call a subroutine?
- [ ] GOTO
- [x] GOSUB
- [ ] RUN
- [ ] CALL
> **Explanation:** The `GOSUB` command is used in BASIC to call a subroutine, specifying the line number where the subroutine begins.
### In programming, what term is often used interchangeably with subroutine?
- [ ] Constructor
- [ ] Destructor
- [x] Procedure
- [ ] Wrapper
> **Explanation:** "Procedure" is a term that is often used interchangeably with subroutine in various programming languages.
### Which statement marks the end of a subroutine and returns control back to the main program in BASIC?
- [ ] END
- [x] RETURN
- [ ] EXIT
- [ ] STOP
> **Explanation:** The `RETURN` statement is used in BASIC to mark the end of a subroutine and return control back to the main program.
### A subroutine in C++ is also known as a?
- [ ] Class
- [ ] Header
- [x] Function
- [ ] Member
> **Explanation:** In C++, a block of code similar to a subroutine is known as a "function."
### In Python, how is a subroutine typically defined?
- [ ] begin
- [ ] sub
- [x] def
- [ ] create
> **Explanation:** A subroutine in Python is typically defined using the `def` keyword.
### Functions in most programming languages:
- [x] May return a value
- [ ] Always return a value
- [ ] Never return a value
- [ ] Must return a list
> **Explanation:** Functions in most programming languages may return a value, although it is not mandatory.
### In object-oriented programming, a subroutine associated with an object is called?
- [ ] Function
- [ ] Routine
- [x] Method
- [ ] Procedure
> **Explanation:** In object-oriented programming, a subroutine associated with an object is called a method.
### What is generally not a characteristic of a subroutine?
- [ ] Encapsulates specific functionality
- [ ] Can be reused
- [ ] Has a unique name
- [x] Increases code complexity
> **Explanation:** Subroutines typically encapsulate specific functionality, can be reused, and have unique names. They generally decrease code complexity, not increase it.
### Which of the following BEST describes the concept of subroutines?
- [ ] They are blocks of code that are executed only once during the program execution.
- [x] They are reusable blocks of code that perform a specific task.
- [ ] They increase the redundancy of code in the software.
- [ ] They are used only in low-level programming languages.
> **Explanation:** Subroutines are reusable blocks of code designed to perform a specific task, thus enhancing code efficiency and maintainability.
Thank you for exploring the fundamentals of subroutines in computer programming. Continue to delve into these concepts to enhance your understanding and application of efficient coding practices.