subroutine
C2Pronunciation
UK
- /sˈʌbruːtˌiːn/
US
- /ˈsəbruˌtin/
Description
- small program within a larger one
- named set of instructions
- reusable code block
- helper part of a larger program
Imagine you're building with LEGO bricks. You might create a small, self-contained structure—like a little car or a tower—that you then use repeatedly as part of a much bigger creation, perhaps a whole city! That smaller, modular structure is very much like a subroutine in computer programming.
A subroutine is essentially a mini-program nestled within a larger program. It is a set of instructions designed to perform one specific task. Programmers write subroutines so they don't have to rewrite the same code over and over again. Instead, they can simply "call" the subroutine whenever that task needs to be executed. Think of it as a shortcut for common actions. While subroutines are often referred to as functions or procedures, their core purpose remains the same: to make code cleaner, more efficient, and much easier to manage.
A subroutine is a self-contained sequence of instructions within a larger computer program that performs a specific, well-defined task. As a fundamental concept in software development, subroutines are designed to promote modularity and efficiency. Think of a subroutine as a specialized building block: you design and refine the block once, then reuse it as many times as necessary throughout your construction.
The primary advantage of subroutines is reusability. Instead of duplicating the same logic every time a specific action is required, a programmer defines that action once as a subroutine and then "calls" or "invokes" it from various points in the main program. This practice significantly reduces the likelihood of errors and makes the code much easier to maintain. For instance, if you need to update how a calculation is performed, you only need to change the code within the subroutine itself; every part of the program that uses that subroutine will automatically benefit from the update.
Depending on the programming language and context, subroutines are known by several names, including functions, procedures, or methods. They are highly versatile, often accepting input values (known as arguments) and returning output values, which allows them to handle dynamic data.
For example, a financial application might feature a subroutine specifically designed to calculate sales tax based on an item's price and local tax rates. This subroutine is triggered every time a new item is added to a digital shopping cart. Similarly, a video game might use a subroutine to render a character's movement on the screen, calling that same block of code every time the player moves the joystick. Ultimately, subroutines are the essential workhorses of complex software, breaking down massive problems into manageable pieces and allowing developers to build sophisticated, organized, and powerful applications.
Examples
- 1
Password check
The program uses a subroutine to check whether the user's password is valid.
- 2
Error handling
If the file is empty, the main code calls a subroutine that shows an error message.
Pattern
call a subroutine
run it from another part of the program
- 3
Reusable code
Instead of writing the same code three times, she put it into a reusable subroutine.
- 4
Program testing
We broke the task into smaller subroutines so the program would be easier to test.
Forms and spellings
2 forms open this card.
Main spelling
- subroutinenoun
Forms
- subroutinespluralnoun