111

(Assuming a single-threaded environment)

A function that fulfills this criterion is:

bool MyClass::is_initialized = false;

void MyClass::lazy_initialize() { if (!is_initialized) { initialize(); //Should not be called multiple times is_initialized = true; } }

In essence, I can call this function multiple times and not worry about it initializing MyClass multiple times

A function that does not fulfill this criterion might be:

Foo* MyClass::ptr = NULL;

void initialize() { ptr = new Foo(); }

Calling initialize() multiple times will cause a memory leak

Motivation

It would be nice to have a single concise word to describe this behavior so that functions that are expected to meet this criterion can be duly commented (especially useful when describing interface functions that are expected to be overridden)

Rufus
  • 1,507

6 Answers6

255

This type of function / operation is called Idempotent

Idempotence (UK: /ˌɪdɛmˈpoʊtəns/,[1] US: /ˌaɪdəm-/)[2] is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.

In mathematics, this means that if f is idempotent, f(f(x)) = f(x), which is the same as saying ff = f.

In computer science, this means that if f(x); is idempotent, f(x); is the same as f(x); f(x);.

Note: These meanings seem different, but under the denotational semantics of state, the word "idempotent" actually has the same exact meaning in both mathematics and computer science.

Rufus
  • 1,507
50

The precise term for this (as Woofas mentions) is idempotence. I wanted to add that while you could call your func1 method idempotent, you could not call it a pure function. The properties of a pure function are two: it must be idempotent and it must not have side effects, which is to say, no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams.

The reason I mention this is that a idempotent function with side effects is not good either, since technically idempotent refers to the return output of the function, and not to the side effects. So technically your func2 method is idempotent, as the output doesn't change according to the input.

You most likely want to specify that you want a pure function. An example of a pure function might be as follows:

int func1(int var)
{
    return var + 1;
}

More reading can be found in the Wikipedia article "Pure function".

gerrit
  • 1,368
Neil
  • 22,848
6

The Term is Idempotence. Note below that there is a distinct difference between an Idempotent function (Called recursively on itself; Second code block and the Mathematical definition), and functional idempotence (Called repeatedly with same input sequentially; First code block and often the term meant in Programming).

A function f with side effects is said to be idempotent under sequential composition f; f if, when called twice with the same list of arguments, the second call has no side effects and returns the same value as the first call[citation needed] (assuming no other procedures were called between the end of the first call and the start of the second call).

For instance, consider the following Python code:

x = 0

def setx(n):
    global x
    x = n

setx(5)
setx(5)

Here, setx is idempotent because the second call to setx (with the same argument) does not change the visible program state: x was already set to 5 in the first call, and is again set to 5 in the second call, thus keeping the same value. Note that this is distinct from idempotence under function composition f ∘ f. For example, the absolute value is idempotent under function composition:

def abs(n):
    if n < 0:
        return -n
    else:
        return n

abs(-5) == abs(abs(-5)) == abs(5) == 5
Tezra
  • 238
4

In addition to the other answers, if there is a specific input to the functon that has this property, it is a fixed point, invariant point or fixpoint of the function. For example, 1 to any power is equal to 1, so (1ⁿ)ⁿ = 1ⁿ = 1.

The special case of a program that produces itself as output is a quine.

Davislor
  • 1,563
3

In physics I've heard this referred to as a projection:

a projection is a linear transformation P from a vector space to itself such that P2 = P. That is, whenever P is applied twice to any value, it gives the same result as if it were applied once (idempotent).

Graphically, this makes sense if you look at a cartoon of a vector projection:

enter image description here

In the picture, a1 is the projection of a on to b, which is like the first application of your function. Subsequent projections of a1 on to b give the same result a1. In other words, when you call a projection repeatedly, it has the same effect as calling it once.

Fair warning: I've never heard this used outside of physics, so unless you've got of those types on your team you might confuse everyone.

3

It is a Deterministic algorithm because given the same input (in this case no input), it will always produce the same output.

In computer science, a deterministic algorithm is an algorithm which, given a particular input, will always produce the same output, with the underlying machine always passing through the same sequence of states. Deterministic algorithms are by far the most studied and familiar kind of algorithm, as well as one of the most practical, since they can be run on real machines efficiently.

SQL databases are interested in Deterministic functions.

A deterministic function always gives the same answer when it has the same inputs. Most built-in SQL functions in SQLite are deterministic. For example, the abs(X) function always returns the same answer as long as its input X is the same.

A function must be deterministic if it's used in calculating an index.

For instance, in SQLite, the following non-deterministic functions cannot be used in an index: random(), changes(), last_insert_rowid() and sqlite3_version().