Lazarus

Free Pascal => Beginners => Topic started by: pascal111 on June 27, 2022, 11:59:24 pm

Title: Procedure declaration before implementation section
Post by: pascal111 on June 27, 2022, 11:59:24 pm
Is it correct to put a procedure declaration before implementation section and why is it acceptable to ignore procedure declaration and just work with its copy that's within implementation section?

Compiler accepted next code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.  
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25.  
  26. //procedure x_proc;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormCreate(Sender: TObject);
  35. begin
  36.  
  37. end;
  38.  
  39.  
  40. procedure x_proc;
  41. begin
  42.  
  43. end;
  44.  
  45.  
  46.  
  47. end.
  48.  
  49.  
Title: Re: Procedure declaration before implementation section
Post by: Thausand on June 28, 2022, 12:53:59 am
Is it correct to put a procedure declaration before implementation section
yes.

Quote
.. and why is it acceptable to ignore procedure declaration and just work with its copy that's within implementation section?
It just "private" function to unit implementation.

More read here https://wiki.freepascal.org/Interface and here https://www.freepascal.org/docs-html/ref/refse112.html#x233-25700016.2
Title: Re: Procedure declaration before implementation section
Post by: PascalDragon on June 28, 2022, 01:43:03 pm
Is it correct to put a procedure declaration before implementation section and why is it acceptable to ignore procedure declaration and just work with its copy that's within implementation section?

You only need to declare a function or procedure in the interface-section (which is the section before the implementation-section) if you want the routine to be accessible from another unit.
Title: Re: Procedure declaration before implementation section
Post by: Zvoni on June 28, 2022, 02:17:26 pm
Is it correct to put a procedure declaration before implementation section and why is it acceptable to ignore procedure declaration and just work with its copy that's within implementation section?

You only need to declare a function or procedure in the interface-section (which is the section before the implementation-section) if you want the routine to be accessible from another unit.
.... or if you need a forward-declaration (for whatever reason) within the same unit?
Title: Re: Procedure declaration before implementation section
Post by: pascal111 on June 28, 2022, 04:33:18 pm
Thanks for you all guys!
I think I got it.
Title: Re: Procedure declaration before implementation section
Post by: MarkMLl on June 28, 2022, 06:13:10 pm
.... or if you need a forward-declaration (for whatever reason) within the same unit?

I think I'd argue that you shouldn't put something in the interface section unless you really need to export it, in the same way that you shouldn't put something in the public section of a class unless you are prepared to take the consequences of its being called under conditions you didn't anticipate.

For OP's benefit: if you find yourself reading about forward declarations in anything other than the FPC documentation, different variants of Pascal (i.e. different compilers and potentially different modes) have two different conventions:

a) Some, notably older, define the parameter list at the point of the forward declaration and omit it where the procedure/function is actually defined.

b) Others require that both have an identical parameter list.

MarkMLl
Title: Re: Procedure declaration before implementation section
Post by: Warfley on June 29, 2022, 10:19:27 am
I think I'd argue that you shouldn't put something in the interface section unless you really need to export it, in the same way that you shouldn't put something in the public section of a class unless you are prepared to take the consequences of its being called under conditions you didn't anticipate.
While generally good advice, this only works until you start using generics:
Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. generic procedure Bar<T>;
  4.  
  5. implementation
  6.  
  7. procedure Foo;
  8. begin
  9.  
  10. end;
  11.  
  12. generic procedure Bar<T>;
  13. begin
  14.   Foo; //  unit1.pas(19,4) Error: Generic template in interface section references symbol in implementation section
  15. end;
Here you have to export Foo in the interface because otherwise you can't use it in the generic function
Title: Re: Procedure declaration before implementation section
Post by: pascal111 on June 29, 2022, 10:52:39 am
It's good mentioning for "generic paradigm", I would ask if free pascal supporting this style of programming.

I think beginners tend to declare everything as public, in classes, functions, procedures, variables ... etc, this makes me remember the first days of BASIC programming language that everything there is public or global, we had no local data.
Title: Re: Procedure declaration before implementation section
Post by: MarkMLl on June 29, 2022, 11:23:31 am
It's good mentioning for "generic paradigm", I would ask if free pascal supporting this style of programming.

Why would he have given you an example if it didn't?

MarkMLl
Title: Re: Procedure declaration before implementation section
Post by: pascal111 on June 29, 2022, 12:58:41 pm
It's good mentioning for "generic paradigm", I would ask if free pascal supporting this style of programming.

Why would he have given you an example if it didn't?

MarkMLl


"Why would he have given you an example if it didn't?!"  8)
Title: Re: Procedure declaration before implementation section
Post by: Thaddy on June 29, 2022, 01:03:39 pm
If you specialize in the interface section, the implementation part works OK.
Title: Re: Procedure declaration before implementation section
Post by: PascalDragon on June 29, 2022, 01:41:24 pm
Is it correct to put a procedure declaration before implementation section and why is it acceptable to ignore procedure declaration and just work with its copy that's within implementation section?

You only need to declare a function or procedure in the interface-section (which is the section before the implementation-section) if you want the routine to be accessible from another unit.
.... or if you need a forward-declaration (for whatever reason) within the same unit?

Then you use a forward-declaration instead of making it public for all users of the unit.

If you specialize in the interface section, the implementation part works OK.

You can't specialize a generic routine in the interface-section.
Title: Re: Procedure declaration before implementation section
Post by: Thaddy on June 29, 2022, 02:23:41 pm
Of course you can, Sarah:
Code: Pascal  [Select][+][-]
  1. unit naamloosunit;
  2. {$mode delphi}
  3. interface
  4. type
  5.    TspecializeMe = TArray<Integer>; // specialized
  6. implementation
  7. end.
Title: Re: Procedure declaration before implementation section
Post by: PascalDragon on June 30, 2022, 01:52:25 pm
Of course you can, Sarah:
Code: Pascal  [Select][+][-]
  1. unit naamloosunit;
  2. {$mode delphi}
  3. interface
  4. type
  5.    TspecializeMe = TArray<Integer>; // specialized
  6. implementation
  7. end.

Learn to read (emphasis mine):

You can't specialize a generic ROUTINE in the interface-section.

Warfley's example was about a generic routine, and not a generic type.
Title: Re: Procedure declaration before implementation section
Post by: pascal111 on June 30, 2022, 05:08:07 pm
Ok guys!
It seems I have a problem, I was testing implementing a function within the implementation section without declaring it in interface section and I met an error as it's shown in the image.

This device has Windows 11.

The code is this one:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject);
  16.   private
  17.  
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.FormCreate(Sender: TObject);
  32. begin
  33.  
  34. end;
  35.  
  36. foo():int;
  37. begin
  38.  
  39. result:=888;
  40. end;
  41.  
  42. end.
  43.  
  44.  
Title: Re: Procedure declaration before implementation section
Post by: pascal111 on June 30, 2022, 05:13:37 pm
I fix it, I used C style, this is the error!

Code: Pascal  [Select][+][-]
  1. function foo():integer;
  2. begin
  3.  
  4. result:=888;
  5. end;  
  6.  
TinyPortal © 2005-2018