The Abstraction
Principle states:
square(2 * 2); // = 16
A function returns a useful value, while a procedure causes a side effect such as updating a variable or printing something.
In the example below getCount is a function while incCount and printCount are procedures:
class Counter {
private int count = 0;
public int getCount() { return count; }
public void incCount() { count = count +
1; }
public void printCount() {
System.out.println("count = " + count); }
}
In Java (and other languages) procedures usually have a return type of void. (Scala uses Unit.)
A hybrid function returns a useful value and causes a side effect:
int getCount() {
System.out.println("count = "
+ count);
return count;
}
Hybrids should be avoided because their side effects can be unwanted. For example, they can interfere with user interfaces.