# Function

A function is a way to store a set of instructions for re-use in a computer program. Functions are passed arguments as input and provide a return value as output.

A function which only interacts with it's arguments and no other data is known
as a *pure function*. Functions which rely on other data are known as
methods.

Pure functions tend to be easier to understand, as they do not change their behavior based upon the state of the system.

Because methods rely on outside variables to do their work, if those variables change the methods behavior may change at runtime.

Some examples:

(in Ruby)

```
# This is a pure function. It is completely self contained.
# It takes an argument named "number", multiplies the number by two and returns
# the result
def double(number)
number * 2
end
@multiplication_amount = 10
# This is a method. It reaches outside of itself for the value stored in
# @multiplication_amount to do it's work.
def configurable_multiplier(number)
number * @multiplication_amount
end
puts configurable_multiplier(5) == 50
@multiplication_amount = 5
puts configurable_multiplier(5) == 25
# See how changing a variable changes the output of the method despite the
# input being the same?
```

(In JavaScript)

```
// This is a pure function. It is completely self contained.
// It takes an argument named "number", multiplies the number by 2, and returns
// the result.
var double = function(number) {
return number * 2;
};
var multiplicationAmount = 10;
// This is a method. It reaches outside of itself for the value stored in
// multiplicationAmount to do it's work.
var configurableMultiplier = function(number) {
return multiplicationAmount * number;
}
console.log(configurableMultiplier(5) == 50);
multiplicationAmount = 5;
console.log(configurableMultiplier(3) == 15);
```