1

Example in C, Static variables have a property of preserving their value even after they are out of their scope. Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

How can I achieve the same functionality in Ada language where the variable is ensured to live until the end of the progran but its scope should be limited to the function hence also ensuring data coupling?

Akay
  • 1,092
  • 12
  • 32
  • Packages. They export the function but any variables declared in either the private part or the package body are only visible to those functions. (This is just one use for a package.) –  Apr 22 '16 at 13:38
  • What if want to make the variable visible to one function only inside the package, not to all the functions in the same package? – Akay Apr 22 '16 at 17:40
  • 1
    Nobody's stopping you giving that function its own package ( or child package) if that's what you need. –  Apr 22 '16 at 17:57
  • If you declare the variable in the package body, it’s only visible to subprograms declared after it. Which goes part of the way. – Simon Wright Apr 23 '16 at 17:19

1 Answers1

1

Variables declared directly in package specifications and bodies retain their state as long as the program is running.

You can't keep variables local to a subprogram as such, but you can declare a single subprogram inside a package, and declare the persistent variables in the body of the package.

Jacob Sparre Andersen
  • 6,733
  • 17
  • 22