Possible Duplicate:
What is a “side effect?”
I don’t understand the wikipedia article on Side Effects:
In computer science, a function or expression is said to have a side effect if, in addition to returning a value, it also 1.) Modifies some state or 2.) Has an observable interaction with calling functions or the outside world.
I know an example of the first thing that causes a function or expression to have side effects – modifying a state
Function and Expression modifying a state
:
1.)
foo(int X)
{
return x = x % x;
}
a = a + 1;
What does 2.) – Has an observable interaction with calling functions or the outside world,” mean? – Please give an example.
The article continues on to say, “For example, a function might modify a global or static variable,
modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions….” Are all these examples, examples of 1.) – Modifiying some state , or are they also part of 2.) – Has an observable interaction with calling functions or the outside world?
1
First, your example is wrong (unless x
refers to a field, and X
is not used). foo(int x) { return x = x % x; }
is actually a pure method, i.e. it doesn’t have side effects. What would have side effects is a method like:
private int x;
public void ComputeValue()
{
this.x = this.x * 2 + 1; // Modifies a state of this.x
}
As for the second point, the outside world can be any element with which your app interacts: file system, a database, user display, printer, USB port, web service, user settings (like the desktop background), etc.
Some examples of methods which interact with the outside world:
public void SayHello()
{
Console.WriteLine("Hello World!");
}
interacts with the console. In other words, SayHello
has a side effect to change the console state by adding characters to it (and optionally moving the previous text to the top, removing the oldest line from the user).
public void SaveHello()
{
File.Save(this.fileName, "Hello World");
}
interacts with the file system. In other words, SaveHello
has a side effect to alter the contents of a file (or create a file).
Think about side effects as something which changes something somewhere outside the method itself. If a method adds a row to a database, the database state changes: this is a side effect. If a method logs an event, the state of the log changes.
Understanding those side effects is crucial when doing parallel computing. A pure method, i.e. a method without side effects, can be run by multiple cores of the CPU at the same time without any problem. A method which changes the state of something must be used with caution. Often, locks and transactions are required to ensure that the program will run as expected.
7
A proper function makes no changes in the environment. It receives arguments, computes a value, and returns it. Anything else is a side effect.
Technically, a function takes some time which you could observe passing, and that could be called a side effect too, so in the real world there is no such thing as “no side effects”.
2