Why does the compiler skip variable assignment

I have the following procedure:

procedure GetDegree(const num : DWORD ; var degree : DWORD ; min ,sec : Extended); begin degree := num div (500*60*60); min := num div (500*60) - degree *60; sec := num/500 - min *60 - degree *60*60; end; 

After assigning the degree variable, the debugger proceeds to the end of the procedure. Why is this?

+7
source share
1 answer

This is an optimization. The min and sec variables are passed by value. This means that changes to them are not visible to the caller and are closed to this procedure. Therefore, the compiler may decide that assigning them is pointless. The values ​​assigned to variables can never be read. Therefore, the compiler chooses to save time and skip tasks. I expect you to declare a procedure as follows:

 procedure GetDegree(const num: DWORD; var degree: DWORD; var min, sec: Extended); 

As I said in the previous question, it is not very important to use Extended . You will be better off with one of the standard floating point types, Single or Double . Or even using the generic Real , which maps to Double .

In addition, you declared min for the floating point type, but the calculation evaluates to an integer. My answer to your previous question is pretty accurate in this regard.


I would recommend creating an entry to store these values. Passing three separate variables makes your function interfaces very messy and breaks encapsulation. These three meanings only make sense when considered as a whole.

 type TGlobalCoordinate = record Degrees: Integer; Minutes: Integer; Seconds: Real; end; function LongLatToGlobalCoordinate(const LongLat: DWORD): TGlobalCoordinate; begin Result.Degrees := LongLat div (500*60*60); Result.Minutes := LongLat div (500*60) - Result.Degrees*60; Result.Seconds := LongLat/500 - Result.Minutes*60 - Result.Degrees*60*60; end; function GlobalCoordinateToLongLat(const Coord: TGlobalCoordinate): DWORD; begin Result := Round(500*(Coord.Seconds + Coord.Minutes*60 + Coord.Degrees*60*60)); end; 
+17
source

All Articles