What is "function"
Of Wikipedia:
The subroutine (also known as procedure, function, routine, method, or subprogram) is a portion of code within a larger program that performs a specific task and is relatively independent of the rest of the code.
The content of a subroutine is its body, which is executed when the subroutine is called or invoked.
A subroutine can be written in order to receive one or more data values - known as parameters or arguments - from the calling program. It can also return a calculated value to its caller (its return value), or provide multiple result values through parameters out.
In fact, a common use of subroutines is the implementation of mathematical functions, in which the effect of the subroutine is only to calculate one or more results whose values are entirely determined by the parameters passed to the routine. (Examples include calculating the logarithm of a number or the determinant of a matrix).
However, a subroutine call can also have side effects, such as modifying data structures in computer memory, reading or writing to a peripheral device, creating a file, locking the program or the machine, or even delay the execution of the program for a certain time. A subprogram with side effects can return different results each time it is called, even if it is called with the same arguments. An example is a random number function, available in several languages, which returns a seemingly different random number each time it is called. The widespread use of subroutines with side effects is a feature of imperative programming languages.
A subroutine can be encoded so that it can call itself recursively, in one or more locations, in order to accomplish its task. This technique allows the direct implementation of functions defined by mathematical induction and recursive algorithms "divide and conquer".
A subroutine whose goal is to compute a single function with boolean value (i.e., to answer a yes/no question) is called a predicate. In logic programming languages, all subroutines are often called "predicates", since they primarily determine success or failure. For example, any type of function is a subroutine, except main()
.
The function is a common unit of code for most other programming languages.
Function also has a mathematical definition, what is important in computer science and statistics. Mathematical function has relationship 1-1 where an argument always returns the same value. In pure functional languages such as Haskell only mathematical functions are allowed.