What is side effect in C?
Asked Answered
I

3

3

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?

Irrelevant answered 2/6, 2020 at 8:57 Comment(3)
For example, a function modifies a global variable from within its code before it returns.Misgiving
If you pass a variable by reference from a local scope to another scope the variable can be changed, unless the parameter is const type.Undertook
The code within a function resides within the scope of that function, and not in the global scope where the global variable is. If a function only returns a value and does not modify anything outside its scope, then it is called a pure function.Misgiving
F
7

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.

Fabien answered 2/6, 2020 at 9:15 Comment(0)
E
5

There is nothing called "main effect" in the C language.

The formal definition of side effect in the C language (C17 5.1.2.3/2) is rather:

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment.

Plainly, a side effect is:

  • accessing a volatile variable, or
  • writing to any variable,
  • or writing to a file

(Where stdout is likely to regard as a file for the given system.)

This is relevant when determining if/how a compiler is allowed to optimize code (C17 5.1.2.3/4):

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

But also when it comes to determine if an expression has well-defined behavior or not, which is where sequence points come in. Sequence points is however a big topic of its own, explained for example in several answers here: Why are these constructs using pre and post-increment undefined behavior?

Expressivity answered 2/6, 2020 at 9:10 Comment(0)
L
1

A State is the attribute of a program which tells about the behavior of the program/function/variable.

for example, observe the state of "i" (variable):

int i = 0; // here state of i is only dependent on the value its holding. lets denote the time when i is having a value 0 as State A.
i++; // i = 1, that means its no longer 0 i.e. state changed to state B
i++ // i = 2 . State C
i--; // i = 1, state B
i += 0; // state B

Side effects: In general, when someone talks about side-effects in a function (regardless of the language), they are talking about changes to the state of the program outside of changes to the function parameters and the object itself.

The way I like to visualize side effect:

----------------------
            \ <= side effect
           ----------------

A function with no side effects in C (where we disregard the possibility of a function being a member function) might look like this:

int stateOfP(int a, char *p)
{
  *p = 0;
   return a+1;
}

In this case, the program has modified a location in memory pointed to by p, but as p is a memory location pointed to in an argument, we don’t count that as a side effect.

A function without side effects is a good thing, for a few reasons.

First, the absence of side effects makes it easier for the compiler to optimize use of the function.

Second, side effects make it much more difficult to prove the correctness of a program.

Finally, when using multiple threads, particularly in a C program, side effects can have undetermined outcomes. For example, if two threads modify an ordinary global variable in a C program without some special locking mechanism, the program’s outcome is undefined.

What does it look like when a function has side effects? Something like this:

int a = 0;
void stateChange(int p)
{
  a++; // here the function is having side effects as 'a' is not its attribute
  return;
}
Lodmilla answered 2/6, 2020 at 9:26 Comment(2)
what do you mean when you say changes to the object itself ??Oscilloscope
Here the state of the object means the state of the function itself.Lodmilla

© 2022 - 2024 — McMap. All rights reserved.