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
Contribute to Docs
- 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.
Learn more on Codecademy
- Skill path
Code Foundations
Start your programming journey with an introduction to the world of code and basic concepts.Includes 5 CoursesWith CertificateBeginner Friendly4 hours - Free course
Learn How to Code
New to coding? Start here and learn programming fundamentals that can be helpful for any language you learn.Beginner Friendly2 hours