Update
This commit is contained in:
parent
f19efc7b70
commit
5604db85d2
24 changed files with 2048 additions and 2026 deletions
13
function.md
13
function.md
|
@ -1,8 +1,8 @@
|
|||
# Function
|
||||
|
||||
Function is a very basic term in [mathematics](math.md) and [programming](programming.md) with a slightly different meanings in each, also depending on exact context: mathematical function basically maps [numbers](number.md) to other numbers, a function in programming is similar but is rather seen as a subprogram to which we divide a bigger program. Well, that's pretty simplified but those are the very rough ideas. A more detailed explanation will follow.
|
||||
Function is a very basic term in [mathematics](math.md) and [programming](programming.md) with a slightly different meanings in each, also depending on exact context: mathematical function basically maps [numbers](number.md) to other numbers, a function in programming is similar but is rather seen as a subprogram to which we divide a bigger program. Well, that's a pretty simplified gist of it but it's roughly how it is. Incoming is a more detailed explanation.
|
||||
|
||||
Yet another attempt at quick summary: imagine function as a tiny box. In mathematics you throw numbers (or similar object, for example [sets](set.md)) to the box and it spits out other numbers (or "objects"); the number that falls out always only depends on the number you throw in. So the box basically just transforms numbers into other numbers. In programming a function is similar, it is also a box to which you throw numbers and can behave like the mathematical function, but the limitations are relaxed so the box can also do additional things when you throw a number in it, it may for example light up a light bulb; it may also remember things and sometimes spit out a different number when you throw in the same number twice.
|
||||
Yet another attempt at a quick summary: imagine function as a miniature box. In mathematics you throw numbers (or similar object, for example [sets](set.md)) into the box and it spits out other numbers (or "objects"); the number that falls out always only depends on the number you throw in. So the box essentially just transforms numbers into other numbers. In programming a function is similar, it is also a box into which you throw numbers and can behave like the mathematical function, but the limitations are relaxed so the box can also do additional stuff, it may for example light up a light bulb; it may also remember things and sometimes shit out a different number when you throw in the same number twice -- sometimes the box is so fancy that it doesn't even need any input numbers anymore, it's just turned on with a button and it starts going around and doing stuff.
|
||||
|
||||
## Mathematical Functions
|
||||
|
||||
|
@ -95,7 +95,7 @@ Mathematical functions can be seen as [models of computation](model_of_computati
|
|||
|
||||
Functions commonly used in mathematics range from the trivial ones (such as the constant functions, *f(x) = constant*) to things like trigonometric functions ([sine](sin.md), [cosine](cos.md), [tangent](tan.md), ...), [factorial](factorial.md), [logarithm](log.md), [logistic](logistic_function.md) sigmoid function, [Gaussian function](gaussian_function.md) etc. Furthermore some more complex and/or interesting functions are (the term function may be applied liberally here):
|
||||
|
||||
- **[Ackermann function](ackermann_function.md)**: Extremely fast growing function with some weird properties.
|
||||
- **[Ackermann function](ackermann_function.md)**: Grows fast as hell and has some weird properties.
|
||||
- **[busy beaver function](busy_beaver.md)**: A famous, extremely quickly growing [uncomputable](computability.md) function.
|
||||
- **Minkowski's [questionmark function](questionmark_function.md)**: Weird function with [fractal](fractal.md) properties.
|
||||
- **sin(1/x)**: Simple function that gets [chaotic](chaos.md) close to zero.
|
||||
|
@ -114,7 +114,7 @@ Functions commonly used in mathematics range from the trivial ones (such as the
|
|||
|
||||
## Programming Functions
|
||||
|
||||
In programming the definition of a function is less strict, even though some languages, namely [functional](functional.md) ones, are built around purely mathematical functions -- for distinction we call these strictly mathematical functions **pure**. In traditional languages functions may or may not be pure, a function here normally means a **subprogram** which can take parameters and return a value, just as a mathematical function, but it can further break some of the rules of mathematical functions -- for example it may have so called **[side effects](side_effect.md)**, i.e. performing additional actions besides just returning a number (such as modifying data in memory which can be read by others, printing something to the screen etc.), or use randomness and internal states, i.e. potentially returning different numbers when invoked (called) multiple times with exactly the same arguments. These functions are called **impure**; in programming a *function* without an adjective is implicitly expected to be impure. Thanks to allowing side effects these functions don't have to actually return any value, their purpose may be to just invoke some behavior such as writing something to the screen, initializing some hardware etc. The following piece of code demonstrates this in [C](c.md):
|
||||
Programmers, being mere scum compared to mathematicians, define a function less strictly, even though some programming languages, namely [functional](functional.md) ones, are still built around purely mathematical functions -- for distinction we call the "strictly mathematical functions" **pure**. In traditional languages functions may or may not be pure, a function here normally means a **subprogram** which can take parameters and return a value, just as a mathematical function, but it can further break some of the rules of mathematical functions -- for example it may have so called **[side effects](side_effect.md)**, i.e. performing additional actions besides just returning a number (such as modifying data in memory which can be read by others, printing something to the screen etc.), or use randomness and internal states, i.e. potentially returning different numbers when invoked (called) multiple times with exactly the same arguments. These functions are called **impure**; in programming a *function* without an adjective is implicitly expected to be impure. Thanks to allowing side effects these functions don't have to actually return or take any value, their purpose may be to just invoke some behavior such as writing something to the screen, initializing some hardware etc. The following piece of code demonstrates this in [C](c.md):
|
||||
|
||||
```
|
||||
int max(int a, int b, int c) // pure function, returns the greatest of three numbers
|
||||
|
@ -136,3 +136,8 @@ unsigned int pseudoRandom(unsigned int maxValue) // impure function
|
|||
In older languages functions were also called *[procedures](procedure.md)* or *[routines](routine.md)*. Sometimes there was some distinction between them, e.g. in [Pascal](pascal.md) functions returned a value while procedures didn't.
|
||||
|
||||
Just as in mathematics, a function in programming may be [recursive](recursion.md) -- here we define recursion as a function that calls itself.
|
||||
|
||||
## See Also
|
||||
|
||||
- [math](math.md)
|
||||
- [lambda calculus](lambda_calculus.md)
|
Loading…
Add table
Add a link
Reference in a new issue