9

I'm coming from C/C++ on embedded systems and all the time inside a function we use a static variable so that the value is retained throughout calls.

In Ada, it seems like this is only done with the equivalent of file-level static variables. Is there an Ada equivalent.

C++:

function Get_HW_Counter() {
   static int count = 0;
   return ++count;
}

Ada:??

Awaken
  • 1,243
  • 2
  • 14
  • 26

1 Answers1

11

Package level variables.

Note that packages are not necessarily at file level; you can even create and use a package local to a subprogram if you wish. One use of a package is to create an object and all the methods acting on it (singleton pattern); keeping all details of the object private.

If my understanding of C++ is not too rusty, a close equivalent would be:

package HW_Counter is
   function Get_Next;
private
   count : natural := 0; -- one way of initialising
   -- or integer, allowing -ve counts for compatibility with C++
end HW_Counter;

and that's all the package's customer needs to see.

package body HW_Counter is

   function Get_Next return natural is
   begin
      count := count + 1;
      return count;
   end Get_Next;

begin  -- alternative package initialisation part
   count := 0;
end HW_Counter;

And usage would typically be

   C := HW_Counter.get_next;
Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • You can create a package local to a subprogram, but it'll get elaborated on each entry (so any variables will be reinitialised). – Simon Wright May 05 '13 at 14:28
  • Actually, on re-reading the question, I think these are precisely the equivalent of file-level static variables. So no, I don't believe there is an Ada equivalent of the C. But it's not hard to get the same effect, if a little more tiresome. – Simon Wright May 05 '13 at 14:31
  • In Ada 2012 you don't even need a package-body you could use an expression-function in the private section to implement the accessor. – Shark8 May 05 '13 at 14:40
  • Shark8, I don't think so? How would you get the increment? – Simon Wright May 05 '13 at 16:28
  • He said static... and damnit it's STATIC! -- j/k... You're absolutely right, forgot that... but the accessing can be done in the spec. – Shark8 May 05 '13 at 21:04
  • What are the pros and cons of declaring the actual variable in the private part instead of the body? – Álex May 18 '13 at 10:59
  • I think in this case it could go in the body. In the private part, it allows the package user to see that the package requires (4 bytes of) storage. My perspective may be coloured by the fact that I've been writing Ada for a rather small target recently ... 1KB executable, 128B RAM - http://www.newark.com/texas-instruments/ez430-t2012/msp430f2012-usb-stick-target-board/dp/20M5139 –  May 18 '13 at 11:18
  • @Simon : good point re: packages local to subprograms. But packages aren't necessarily at file scope; they may be local to other packages for example. The above example certainly looks heavyweight next to the C++ example, but its the basis for something more comprehensive : an opaque type exporting only the permitted operations on it. As soon as you want to extend the example to allow one more operation on the variable : e.g. to set the counter to a specific value, it's an easy addition to the package, but the C++ example will end up looking much closer to the Ada version. –  May 18 '13 at 11:30