Subroutine
A subroutine is a set of computer instructions written to perform specific tasks. It is also known as a function or a procedure. A subroutine packages code for an operation or a set of operations, which makes code reusable and readable.
Syntax
Every programming language has its own syntax for defining and calling a subroutine or function. Often, the basic format of a subroutine will follow the model below:
Pseudo code for declaring a subroutine:
declare <subroutine_name>
# subroutine code
end
Pseudo code for calling a subroutine:
call <subroutine_name>
In modern programming languages, we use functions that are similar to subroutines. These functions take in argument(s) and may return some value(s) after execution.
Let’s look at subroutine in JavaScript syntax.
A function, func_name()
, is declared and takes in any number of arguments:
function func_name(/* var1, var2, var3,... */) {// Function body// Return statement is optionalreturn return_value;}
To call or invoke the function with any number of arguments:
func_name(/* var1, var2, var3,... */);
Codebyte Example
Subroutines/Functions in Different Languages
All contributors
- ramanLamichhane87568053396 total contributions
- garanews222 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- Anonymous contributorAnonymous contributor2 total contributions
- ramanLamichhane8756805339
- garanews
- Anonymous contributor
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.