Recent

Author Topic: Procedure declaration before implementation section  (Read 1657 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Procedure declaration before implementation section
« 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.  
La chose par la chose est rappelé.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Procedure declaration before implementation section
« Reply #1 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

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Procedure declaration before implementation section
« Reply #2 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.

Zvoni

  • Hero Member
  • *****
  • Posts: 2300
Re: Procedure declaration before implementation section
« Reply #3 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?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Procedure declaration before implementation section
« Reply #4 on: June 28, 2022, 04:33:18 pm »
Thanks for you all guys!
I think I got it.
La chose par la chose est rappelé.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Procedure declaration before implementation section
« Reply #5 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Procedure declaration before implementation section
« Reply #6 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

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Procedure declaration before implementation section
« Reply #7 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.
La chose par la chose est rappelé.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: Procedure declaration before implementation section
« Reply #8 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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Procedure declaration before implementation section
« Reply #9 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)
La chose par la chose est rappelé.

Thaddy

  • Hero Member
  • *****
  • Posts: 14164
  • Probably until I exterminate Putin.
Re: Procedure declaration before implementation section
« Reply #10 on: June 29, 2022, 01:03:39 pm »
If you specialize in the interface section, the implementation part works OK.
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Procedure declaration before implementation section
« Reply #11 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 14164
  • Probably until I exterminate Putin.
Re: Procedure declaration before implementation section
« Reply #12 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.
« Last Edit: June 29, 2022, 02:26:07 pm by Thaddy »
Specialize a type, not a var.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Procedure declaration before implementation section
« Reply #13 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.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Procedure declaration before implementation section
« Reply #14 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.  
La chose par la chose est rappelé.

 

TinyPortal © 2005-2018