Re: Re: Re: Re: Function syntax


Posted by webmaster Guido on April 11, 2001 at 01:16:43:

In Reply to: Re: Re: Re: Function syntax posted by James on April 07, 2001 at 16:33:38:

You can declare the function in the "public" part of the interface section, if you want to use it also in other units:

unit Unit1;
interface
  ...
  type
    TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    public
      function Returndist(cspeed, fspeed, area, dragcoeff,
       downfcoeff, gripcoeff, totcarweight: double): double;
    end;
  ...

implementation
...
function TForm1.Returndist(cspeed, fspeed, area, dragcoeff,
  downfcoeff, gripcoeff, totcarweight: double): double;
begin
  ...
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  S1, S2, A, Dr, DrC, GrC, TCW: double;
begin
  ...
  while Returndist(S1, S2, A, Dr, DrC, GrC, TCW) > Distance do
  ...
end;

If you declare the function in the "private" section, then other units can *not* see it. Only the interface section is modified:

unit Unit1;
interface
  ...
  type
    TForm1 = class(TForm)
      Button1: TButton;
      procedure Button1Click(Sender: TObject);
    Private
      function Returndist(cspeed, fspeed, area, dragcoeff,
       downfcoeff, gripcoeff, totcarweight: double): double;
  end;
...

implementation
...

In the examples above, the function is declared in the interface section as a "method" of the form type TForm1. But if the function is only needed in the unit itself, you even don't have to declare it at all in the interface section. Just make sure that you write it in the implementation section *before* any routine that calls the function, like this: (note that I left out the "Tform1." part!)

unit Unit1;
interface
...
 type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;
 ...

implementation
...
function Returndist(cspeed, fspeed, area, dragcoeff,
   downfcoeff, gripcoeff, totcarweight: double): double;
begin
  ...
end;

procedure TForm1.Button1Click(Sender: TObject);
 var
  S1, S2, A, Dr, DrC, GrC, TCW: double;
begin
  ...
  while Returndist(S1, S2, A, Dr, DrC, GrC, TCW) > Distance do
    ...
end;
-------


: JC - I did do what you suggested, and although I do think that was a additional think that was wrong, I still get an error message when I compile.
: It gets stuck right at the top of the Unit, on the line where Ive said:
: function Returndist(cspeed: Double; fspeed: Double; area: Double; dragcoeff: Double; downfcoeff: Double; gripcoeff: Double; totcarweight: Double) : Double;
: And comes up with the error message: "Unsatisfied forward or external declaration: 'TForm1.Returndist' "
: I'd really appreciate it if you could tell me what is wrong with this line, and how to fix it if possible.
: Have I not got this line in the right place? (just under the procedure prototypes)
: Thanks a lot
: James
: : Thanks :)
: : Ill try that
: : James
: : : Try
: : : ...
: : : While (returndist(cspeed, fspeed, area, dragcoeff, downfcoeff, gripcoeff, totcarweight) > distance) do
: : : ...


Related Articles and Replies:


Related Articles and Replies    Q & A Forum    DelphiLand Home