Thank you both for your helpful reply!

I have remove the 'collisionvalue' line from the code and changed to Handoko's piece of code and I corrected the typo as Eugene Loza suggested. It seems, now the function gives correct values without errors.

Otherwise, I tried another method, but it showed slightly different values:

bigrectanglex1:=min(x1,x2);

bigrectangley1:=min(y1,y2);

bigrectanglex2:=max(x1+w1,x2+w2);

bigrectangley2:=max(y1+h1,y2+h2);

xdist:=(bigrectanglex2-bigrectanglex1)-(w1+w2);

ydist:=(bigrectangley2-bigrectangley1)-(h1+h2);

innerrectx1:=max(x1,x2)-xdist;

innerrecty1:=max(y1,y2)-ydist;

innerrectx2:=max(x1,x2);

innerrecty2:=max(y1,y2);

distance:=pointdistance(innerrectx1,innerrecty1,innerrectx2,innerrecty2);

It is based on this Google suggestion:

"Combine the two rectangles into one large rectangle. Subtract from the large rectangle the first rectangle and the second rectangle. What is left after the subtraction is a rectangle between the two rectangles, the diagonal of this rectangle is the distance between the two rectangles."