Microsoft Store
 

Subroutine


 

In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one or more statement blocks; such code is sometimes collected into software libraries. Subroutines can be "called", thus allowing programs to access the subroutine repeatedly without the subroutine's code having been written more than once. In many programming languages the word function is reserved for subroutines that return a value; however, the C programming language and its programmers view subroutines simply as functions that do not return a value.

C and C++ examples

In the C and C++ programming languages, subprograms are referred to as "functions". Note that these languages use the special keyword void to indicate that a function does not return any value — that is it only has side-effects.

Related Topics:
C - C++

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Below are three such functions - the first function does absolutely nothing; it is called with: "function1();. The second function returns the number 5; the function can be called with: "function2();" The third function returns a desired selection (1-5), and is called with: "function3(number);".

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

void function1(void)

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

}

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

int function2(void)

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

return 5;

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

}

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

int function3(int number)

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

int selection = {5,1,3,2,4};

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

return selection;

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

}

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

~ ~ ~ ~ ~ ~ ~ ~ ~ ~