-
Book Overview & Buying
-
Table Of Contents
TypeScript Blueprints
By :
When you ask a developer what the definition of a function is, he would probably answer something like "something that does something with some arguments". Mathematicians have a formal definition for a function:
A function is a relation where an input has exactly one output.
This means that a function should always return the same output for the same input. Functional programming (FP) uses this mathematical definition. The following code would violate this definition:
let x = 1;
function f(y: number) {
return x + y;
}
f(1);
x = 2;
f(1);
The first call to f would return 2, but the second would return 3. This is caused by the assignment to x, which is called a side effect. A reassignment to a variable or a property is called a side effect, since function calls can give different results after it.
It would be even worse if a function modified a variable that was defined outside of the function:
let x = 1; function...