Subprograms and Modularity

Private subprograms

We've seen previously that we can declare private packages. Because packages and subprograms can both be library units, we can declare private subprograms as well. We do this by using the private keyword. For example:

private procedure Test;
procedure Test is begin null; end Test;

Such a subprogram as the one above isn't really useful. For example, we cannot write a with clause that refers to the Test procedure, as it's not visible anywhere:

with Test; procedure Show_Test is begin Test; end Show_Test;

As expected, since Test is private, we get a compilation error because this procedure cannot be referred in the Show_Test procedure.

A more useful example is to declare private subprograms of a package. For example:

package Data_Processing is type Data is private; procedure Process (D : in out Data); private type Data is null record; end Data_Processing;
with Data_Processing.Calculate; package body Data_Processing is procedure Process (D : in out Data) is begin Calculate (D); end Process; end Data_Processing;
private procedure Data_Processing.Calculate (D : in out Data);
procedure Data_Processing.Calculate (D : in out Data) is begin -- Dummy implementation... null; end Data_Processing.Calculate;
with Data_Processing; use Data_Processing; procedure Test_Data_Processing is D : Data; begin Process (D); end Test_Data_Processing;

In this example, we declare Calculate as a private procedure of the Data_Processing package. Therefore, it's visible in that package (but not in the Test_Data_Processing procedure).