Subroutine

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 optional
return 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

Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn More on Codecademy

Edit this page on GitHub

Contributors