Subroutine
From Programmer's Wiki
A subroutine is a block of code that can be called later on. Subroutines are also often called functions. Most modern programming languages have built in functions so programmers are less likely to have to "reinvent the wheel". Subroutines help to provide some abstraction to the code. This helps prevent errors as the code is simpler and the number of inputs to it are limited. A subroutine usually has a number of parameters and a return type. Parameters are a type of variable whose scope is inside the subroutine. Parameters allow subroutines to be used in different situations.
Contents |
[edit] Return Type
A return type is used to return some data to the application. Functions without a return type will not return any data, but will still be able to do things such as modify existing data in the application.
[edit] Examples
[edit] Java
Declares a public static function called Addition, with two parameters.
public static double addition(double a, double b){
return a+b;
}
//Function can then be called (actually, it doesn't matter if the function is defined before or after it is called the first time)
System.out.println(addition(1,2)); //Outcome: 3
[edit] Python
Declares a function called addition, with two parameters.
def addition(a,b):
return a+b
#Calling the function
print addition(2,4) #Outcome: 6
