Recent

Author Topic: Declaring procedure in Unit as private  (Read 7619 times)

Bill52

  • New member
  • *
  • Posts: 8
Declaring procedure in Unit as private
« on: August 21, 2014, 03:21:17 pm »
Could someone please give an example how to declare a procedure in a Unit as private? I can't find this in the FreePascal reference guide.

Many thanks,

Bill

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Declaring procedure in Unit as private
« Reply #1 on: August 21, 2014, 03:23:59 pm »
If you want that a procedure be private of a unit, you can simply not declare it.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Declaring procedure in Unit as private
« Reply #2 on: August 21, 2014, 04:04:59 pm »
Perhaps you mean 'hiding' a procedure in the implementation section?

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

// normal interface type declarations etc.

implementation

procedure UnseenOutsideThisUnit;
begin
  ...
end;

end.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Declaring procedure in Unit as private
« Reply #3 on: August 21, 2014, 04:11:17 pm »
In order to make anything private in a unit you declare it in the implementation section as forward and not in the interface.
eg
Code: [Select]
implementation

procedure DoYourThing;forward;

procedure WhatYouWant;
begin
  DoyourThing;
end;

procedure DoYourThing;
begin
  WriteLn('Done');
end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Declaring procedure in Unit as private
« Reply #4 on: August 21, 2014, 06:31:30 pm »
In order to make anything private in a unit you declare it in the implementation section as forward and not in the interface.
eg

The forward declaration is not needed to"hide" the function:

Code: [Select]
unit example;

interface

procedure WhatYouWant;

implementation

procedure DoYourThing;
begin
  WriteLn('Done');
end;

procedure WhatYouWant;
begin
  DoyourThing;
end;

procedure WhatYouWant is visible to anything that has example in it's uses clause.
procedure DoyourThing is only visible to the proceduresand functions in the implementation section of the example unit.

Bart

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Declaring procedure in Unit as private
« Reply #5 on: August 21, 2014, 07:39:52 pm »

The forward declaration is not needed to"hide" the function:

No its not needed to hide the function is needed to declare it.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Declaring procedure in Unit as private
« Reply #6 on: August 21, 2014, 10:26:59 pm »
No its not needed to hide the function is needed to declare it.

But only if the order you wrote them in is absolutely necessary.
(Which, from your example isn't clear.)

Bart

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Declaring procedure in Unit as private
« Reply #7 on: August 21, 2014, 10:34:23 pm »
No its not needed to hide the function is needed to declare it.

But only if the order you wrote them in is absolutely necessary.
(Which, from your example isn't clear.)

Bart

I have no idea if it is "absolutely necessary", I'm answering a question avoiding assumptions, it is a time saver though if you are not interested in finding out if it is absolutely necessary.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bill52

  • New member
  • *
  • Posts: 8
Re: Declaring procedure in Unit as private
« Reply #8 on: August 22, 2014, 10:00:29 am »
Thank you for everybody!

I didn't know, the order of of the procedures under implementation is important.

Thanks again!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Declaring procedure in Unit as private
« Reply #9 on: August 22, 2014, 10:24:17 am »
I didn't know, the order of of the procedures under implementation is important.

Pascal is strictly declare before use. So if you call a function before it is declared (either a declaration
in the interface, or code in the implementation section), you need to declare the procedure header only forward.

This is usually not done as a matter of routine. I only have an handful of forwards in my framework which are hundreds
of units. (though admitted, in OOP code you declare more, so it is less needed)

In case needed look up "forward" in the manual.

 

TinyPortal © 2005-2018