Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is side effect in C?

Wikipedia says that:

In computer science, an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, that is to say has an observable effect besides returning a value (the main effect) to the invoker of the operation.

But how can we access a variable outside its local environment, can anyone explain this situation, side effect, main effect and sequence point comprehensibly?

like image 354
Ulaş Sezgin Avatar asked Apr 16 '26 06:04

Ulaş Sezgin


1 Answers

A function is (should be) a black box, in which the return value, or the value of a variable passed by reference, should be the only thing that may change depending upon the input parameters.

Any other observable change that the function produces outside these cases, is a side-effect. The most well-known example may be the printf() function which, besides returning the number of written characters, changes the contents of the standard output, which means altering some memory buffer associated with a pipe, a file, or the screen, for instance, and which doesn't belong to the local environment of the function.

like image 77
mcleod_ideafix Avatar answered Apr 19 '26 08:04

mcleod_ideafix