Contents - Index


Example .DLF External Function in DELPHI (PWF)

 

The following listing shows the source code for the PWF.DLF external function.  Note that this external routine does not provide units for the inputs and outputs so that there will not be any unit error checking when this  function is called.

______________________________________________________

 

library PWFP;

 

const doExample = -1;

 

type

  CharString = array[0..255] of char;

  ParamRecPtr=^ParamRec;

  ParamRec=record

    Value:double;

    next:ParamRecPtr;

  end;

 

 function CountValues (P: ParamRecPtr): integer;

  var

   N: integer;

  begin

   N := 0;

   while (P <> nil) do begin

     N := N + 1;

     P := P^.next

    end;

   CountValues := N;

  end; {CountValues}

 

function PWF(var S:CharString; var Mode:integer;

          Inputs:ParamRecPtr):double; export; stdcall;

  var

    P: ParamRecPtr;

    V: double;

 

 function CountValues (P: ParamRecPtr): integer;

  var

   N: integer;

  begin

    N := 0;

    while (P <> nil) do begin

      N := N + 1;

      P := P^.next

     end;

    CountValues := N;

  end; {CountValues}

 

 function PWFCalc: double;

  var

    NArgs: integer;

    interest, discount, periods: double;

  begin

    PWFCalc:=0; {in case of error exit}

    S := '';

    P := Inputs;

    Periods := P^.value;

    if (Periods < 0) then begin

      S := 'The number of periods for the PWF function must be >0.';

      exit;

    end;

    P := P^.next;

    interest := P^.value;

    if (interest >= 1) or (interest < 0) then begin

      S := 'The interest rate is a fraction and must be between 0 and 1.';

      exit;

    end;

    P := P^.next;

    discount := P^.value;

    if (discount >= 1) or (discount < 0) then begin

      S := 'The discount rate is a fraction and must be between 0 and 1.';

      exit;

    end;

    if (interest <> discount) then

      PWFCalc := 1 / (discount - interest) * (1 - exp(Periods * ln((1 + interest) / (1 + discount))))

    else

      PWFCalc := Periods / (1 + interest);

  end; {PWF}

 

 begin

   PWF:=1;

   if (Mode = doExample) then begin

      S := 'PWF(Periods,Interest,Discount)';

      exit;

   end;

   if (CountValues(Inputs)<>3) then

     S := 'Wrong number of arguments for PWF function.'

   else begin

     PWF:=PWFCalc; 

   end; 

 end; {PWF}

 

 exports

   PWF;

 

begin

  {no initiation code needed}

end.

 

This file was compiled using DELPHI.  After building the file, it is necessary to rename it to PWF.DLF.