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;
}