February 2018
Intermediate to advanced
350 pages
7h 35m
English
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(10,15)}")
}
The preceding program is a simple object-oriented program. However, it contains side effects. The addNumbers() function modifies ...
Read now
Unlock full access