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 :


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?

  • Thanks for asking your first question. Please take a look at programmers.stackexchange.com/questions/how-to-ask for some helpful suggestions on asking questions on Stack Exchange Programmers. Be as specific as you can and take care that if you introduce an example, that it matches the situation described in the document you use as reference material. – DeveloperDon Sep 2 '12 at 3:24

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.

| improve this answer | |
  • So the reason int foo(int x) { return x = x % x; } does NOT have side effects is becuase 3 % 3 = 3... – Chris Okyen Sep 1 '12 at 20:32
  • int foo(int x ) { return x = x + 1; } has ( referential transparency ) side effects? – Chris Okyen Sep 1 '12 at 20:32
  • 1
    It does not have side-effects because it does not modify any variable external to the function, nor does it print anything on the screen or communicate with the external world in any other way. – Giorgio Sep 1 '12 at 21:09
  • 1
    Besides, 3 % 3 is 0, not 3... – iCanLearn Sep 1 '12 at 21:44
  • 1
    @ChrisOkyen Think of it this way: int foo(x) { return x + 1; } ; int z = 1; ; foo(z) ; At this point, if z and any other variables that exist outside of foo are unchanged, then foo does not have side-effects. It doesn't matter whether or not x is changed locally inside the function. – Izkata Sep 2 '12 at 21:25

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".

| improve this answer | |
  • It also has to not receive information from the environment. Thus a "function" which returns the current time has side effects. – MatrixFrog Sep 2 '12 at 21:38
  • I agree about the strict propriety of the function, but side effects are usually defined as changing the environment. – ddyer Sep 3 '12 at 19:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.