[GAP] Question about subprogram syntax

Riehle, Richard (CIV) rdriehle at nps.edu
Tue Feb 21 22:06:06 CET 2006



The idea of parameterless functions has been quite useful
for me over the years.  Instead of coding constants in a 
specification, I have sometimes coded functions that
return a value and implemented that value as a function.

Here is a sample of what I mean.   

package Sample_Constants is

   function Pi return Float;
   pragma Inline(Pi);
   
   function e return Float;
   pragma Inline(e);
   
   function Farenheit_Freezing return Float;
   pragma Inline(Farenheit_Freezing);
   
end Sample_Constants;


with Ada.Numerics;
package body Sample_Constants is

   function e return Float is
   begin
      return Ada.Numerics.e;
   end e;


   function Farenheit_Freezing return Float is
   begin
      return 32.0;
   end Farenheit_Freezing;


   function Pi return Float is
   begin
      return Ada.Numerics.Pi;
   end Pi;

end Sample_Constants;


This might seem a bit strange for the example shown, but
for real life computing, where the constants are application
specific, it works quite well.   This is one of those cases
where Ada's separation of the specification from the body
is really useful.   There are times when we need more or even
less precision for a constant, and we prefer not to require
recompilation of the package specification each time we change
our minds.   This is especially helpful when the entire system
depends on these constants.  

Richard Riehle

-----Original Message-----
From: gap-bounces at gnat.info [mailto:gap-bounces at gnat.info] On Behalf Of Anthony S Ruocco
Sent: Tuesday, February 21, 2006 11:54 AM
To: gap at gnat.info
Subject: RE: [GAP] Question about subprogram syntax

Maybe we are over-shooting the mark here as we are talking to students, not language aficionados.  I like to tout the power of Ada is in coding what you want to do. Instead of doing a lot of language oriented discussion why not back-step to how we actually use a function, that is

  Y = f(x)  

The function manipulates an input x to yield an output y.  Let f(x) be x+2 then

X	f(x)
1	3
2	5
Etc

If f(x) always returned the same value, then there would not be a function, but a constant.

Now in the code we would use

Y := f(x);  -- which looks a lot like the actual thing we want to do

In the event that f(x) always returned a constant then create a constant and use it

B    : constant int 45;

Y : = B;   --- which still looks a lot like what we want to do, and gives us flexibility in resetting B later.

Personally, I cant see the need for parameter-free subprograms.  Functions and procedures should manipulate something.  And if they are going to manipulate something, then we should control what they manipulate - hence use parameters and at least contain the blast zone.

Tony Ruocco


-----Original Message-----
From: Peter C. Chapin [mailto:pchapin at ecet.vtc.edu] 
Sent: Tuesday, February 21, 2006 10:37 AM
To: GNAT Academic Program discussion list
Subject: Re: [GAP] Question about subprogram syntax


On Tue, 21 Feb 2006, Diego Alonso Cáceres wrote:

> I've been teaching Ada for three years now and often my students make me 
> the same question: "Why don't parameter-less subprograms have empty 
> brackets like C or Java? The code would be easier to read and maintain. 
> It would be also possible to have a variable and a procedure with the 
> same name."
>
> I really don't what to answer because I think the same. Wasn't Ada 
> thought as "write once, read many times" or "Ada enhances readability"? 
> Why inherit this from Pascal?

I'm not an expert but my understanding was that this is considered a 
feature. The idea is that it is possible to replace a constant with a 
parameterless function without editing any code that uses that constant.

 	A := B;

B might be a value today, but a parameterless function tomorrow. This 
might be useful, for example, when it becomes necessary to do some 
significant computation to obtain the desired value.

I don't know how often this actually comes up in the wild, though.

Peter

_______________________________________________
GAP mailing list
GAP at gnat.info
/no-more-mailman.html
To unsubscribe from this list, please contact the GAP GNAT Tracker administrator
within your organization.


More information about the GAP mailing list