Noch nicht registriert? Registrier Dich jetzt!
Ignoring function results
Autor Thema
Profil Webseite
Hi

There are an error if not you are using an function result, the following code (cut and somewhat modified from one of my projects) demonstrate :

Code
  1. function Scale(Ix, Iy : longint; var Dx, Dy : longint) : extended;
  2. var
  3.   wf,
  4.   ff,
  5.   hf : extended;
  6.  
  7. begin
  8.   wf := ix / dx;
  9.   hf := iy / dy;
  10.  
  11.   if (wf > hf) then ff := wf
  12.   else ff := hf;
  13.  
  14.   dx := round(ix / ff);
  15.   dy := round(iy / ff);
  16.  
  17.   result := ff;
  18.  
  19. Procedure TForm1.Button1OnClick (Sender: TObject);
  20. var
  21.   w,
  22.   x,
  23.   y : longint;
  24.   f : extended;
  25.  
  26. Begin
  27.   for w := 402 to 410 do
  28.   begin
  29.     x := 500;
  30.     y := w;
  31.     f := scale(2087,1691,x,y);
  32.     memo1.lines.add(inttostr(w) + ' / ' + inttostr(x) + ' / ' + inttostr(y));
  33.   end;
  34.   memo1.lines.add('**********');
  35.   for w := 402 to 410 do
  36.   begin
  37.     x := 500;
  38.     y := w;
  39.     scale(2087,1691,x,y);
  40.     memo1.lines.add(inttostr(w) + ' / ' + inttostr(x) + ' / ' + inttostr(y));
  41.   end;


Please observe, the only difference between the two parts of the program is the "f :=" before Scale in the first part, but the result is as follows (first part is the correct one) :

402 / 496 / 402
403 / 497 / 403
404 / 499 / 404
405 / 500 / 405
406 / 500 / 405
407 / 500 / 405
408 / 500 / 405
409 / 500 / 405
410 / 500 / 405
**********
402 / 496 / 402
403 / 497 / 403
404 / 499 / 404
405 / 500 / 405
406 / 500 / 405
407 / 500 / 405
408 / 500 / 405
409 / 505 / 409
410 / 506 / 410

And not only the calculations are affected, I have been hunting many wierd errors before I found out this was the culprit.

Kim Foder