I don’t understand the definition of side effects [duplicate]

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>foo(int X)
{
return x = x % x;
}
a = a + 1;
</code>
<code>foo(int X) { return x = x % x; } a = a + 1; </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private int x;
public void ComputeValue()
{
this.x = this.x * 2 + 1; // Modifies a state of this.x
}
</code>
<code>private int x; public void ComputeValue() { this.x = this.x * 2 + 1; // Modifies a state of this.x } </code>
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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public void SayHello()
{
Console.WriteLine("Hello World!");
}
</code>
<code>public void SayHello() { Console.WriteLine("Hello World!"); } </code>
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).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public void SaveHello()
{
File.Save(this.fileName, "Hello World");
}
</code>
<code>public void SaveHello() { File.Save(this.fileName, "Hello World"); } </code>
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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật