A Side effects is something that changes the state of computer or your programme beyond just giving back a value.
Side effects are considered bad, because, a function with side-effects are unpredictable ,depending on the state of the system.
When a function has no side effects we can execute it anytime, it will always return the same result, given the same input.
When we talk about functions we’ve said that its best if your function simply calculate a value
Such functions generally do not have any side-effects. They don’t read anything from input they don’t print anything from output they do a calculation and they give you back the result.
They don’t change anything as they are happening , because they don’t read, print they also never assign to external Variables. So we can’t tell the difference before the function has occurred and after the function has occurred because nothing has been altered on the computer.
These are the most generally useful types functions . But you’re going to write others at various times and it’s worth introducing the concept of a procedure. Which would be a function that you create simply because of its side effects.
Many languages have separate syntax for procedure. But in Scala the return type UNIT signifies procedures.
This simple function ->
- All that it does is side-effects it doesn’t calculate anything for us .
- It doesn’t give us back any value we can’t do more with it
- This function just prints particular output to console
And so when we see something that returns Unit (it’s not giving you back a value) it indicates that the only reason you would call it is for the side-effects that it has .
Whether those side-effects are printing things or doing assignments to “vals”, possibly even reading things in if you just wanted to skip stuff from the input.
Functions are written specifically to read stuff from input , but also return values many times. But we are aware these types of functions are often called procedure in other languages. And the idea of having something that actually changes values is refer to as as side-effects.
We would often try to write our programme in ways that are side-effects free. So that we only do side-effects when absolutely needed .
WHY ARE SIDE EFFECTS NECESSARY
When we write a script and if we don’t have a side effect someplace it’s not doing anything . We have to at-least print a result for it to be useful to a user. And if you don’t print anything the user hasn’t any indication that anything even executed when we run the programme.
So we aren’t avoiding side-effects completely. But we are trying to make it so simple that the side-effects kind of happen at the highest level in our program. And that the majority of the logic that goes on in our program is side-effect free.
LOCAL SIDE EFFECT
Functions with visible mutation are considered as functions with side-effects. But what if a functions that has some side-effects internally also gives same value. for eg:
This function mutates the variable “result” in every iteration in for loop i.e this function holds a side-effect.
Here also, the function for same input returns same output . Because the side effects are not visible to user or has no impact on rest of the code . The function is deterministic, sumsIntegers(5) = 10 and sumsIntegers(10) = 45 etc,
We say that , since it does not break our substitution model(reduce an expression to a value), although the function has a local side effect, user of that function has no issues.
Hence, this function is pure, although it has a local side effect.
Consider another example
Here if we call the function sunIntergers(5) will give us 10 first time , but if we call the function again with the input 5 it will give us value 20
In short , variable mutations are side-effect . Local side-effects are commonly used for better optimisation of code .