Subroutine

Anonymous contributor's avatar
Anonymous contributor
Published Oct 13, 2021Updated Oct 26, 2022
Contribute to Docs

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 optional
return return_value;
}

To call or invoke the function with any number of arguments:

func_name(/* var1, var2, var3,... */);

Codebyte Example

Code
Output
Loading...

Subroutines/Functions in Different Languages

All contributors

Contribute to Docs

Learn more on Codecademy