So, we have learned about lambda and high order functions. They are two of the most interesting and important topics of functional programming. In this section, we will be discussing side effects and pure functions.
So, let's start by defining side effects. We will then gradually move towards pure functions.
In a computer program, when a function modifies any object/data outside its own scope, that is called a side effect. For instance, we often write functions that modify a global or static property, modify one of its arguments, raise an exception, write data to display or file, or even call another function which has a side effect.
For example, have a look at the following program:
class Calc { var a:Int=0 var b:Int=0 fun addNumbers(a:Int = this.a,b:Int = this.b):Int { this.a = a this.b = b return a+b } } fun main(args: Array<String>) { val calc = Calc() println("Result is ${calc.addNumbers...