Search code examples
computer-scienceterminology

What do you call a function that can be called again to re-do instead of doing again?


I'm looking for a term for functions that are not pure and not necessarily reentrant, but when called a second time create the same state as when they were called the first time.

For example, this function would not pass the criteria:

void CounterExample(int value)
{
  static int STORE[5];
  static int STORE_COUNT=0;

  STORE[STORE_COUNT++] = value;
}

While this one would:

void Example(int value)
{
  static int STORE[5];
  static int STORE_COUNT=0;

  STORE_COUNT = 0;
  STORE[STORE_COUNT++] = value;
}

In particular I'm thinking of hardware initialisation functions. Often they would set up GPIO in certain ways, go through the power-on sequence, and then configure the hardware someway. If later I need to re-initialise the hardware, I can just call this function again. If, however, the function also sets up some software structures by, say, appending to an array of configured structures, then I cannot call this function again without first undo-ing what the function had done in the first place.

For example, a comms driver initialisation function might power on the comms driver, send some config commands, and then call another function called AddCommsCallback() which registers a default callback for data received by the comms device. If I ever wanted to reset the comms driver, I cannot simply call this function again because the default callback will be appended to the existing obsolete callback.

Is there specific terminology associated with these two types of functions (other than generic terms of merit!)?


Solution

  • That is an idempotent function.

    Read more under the imaginatively named: "What is an idempotent operation?"