What does this piece of code do?

I found the following piece of code in a folder that involves the use of servos. Can anyone tell me what it is meant to do and how?

int pTerm, dTerm;
int Kp = 3;
int Kd = 5;
int integrated_error = 0;
int last_error = 0;
int GUARD_GAIN = 20;
float K = 1;

int updatePid(int targetPosition, int currentPosition)
{
int error = targetPosition - currentPosition;
pTerm = Kp * error;
dTerm = Kd * (error - last_error);
last_error = error;
return -constrain(K*(pTerm + dTerm), -127, 127);
}

Can anyone tell me what it is meant to do and how?

It uses a process call PID (google that) to, apparently, control the speed of a servo, going fast when far away from the target position, slowing down as it gets closer. Without seeing how the return value is used, it's hard to be certain.