Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ada (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Packages, procedures and functions === Among the parts of an Ada program are packages, procedures and functions. Functions differ from procedures in that they must return a value. Function calls cannot be used "as a statement", and their result must be assigned to a variable. However, since Ada 2012, functions are not required to be pure and may mutate their suitably declared parameters or the global state.<ref>{{cite web |title=Subprograms |url=https://learn.adacore.com/courses/intro-to-ada/chapters/subprograms.html#function-calls |website=learn.adacore.com |publisher=AdaCore |access-date=14 April 2024}}</ref> Example: Package specification (example.ads) <syntaxhighlight lang="ada" line> package Example is type Number is range 1 .. 11; procedure Print_and_Increment (j: in out Number); end Example; </syntaxhighlight> Package body (example.adb) <syntaxhighlight lang="ada" line> with Ada.Text_IO; package body Example is i : Number := Number'First; procedure Print_and_Increment (j: in out Number) is function Next (k: in Number) return Number is begin return k + 1; end Next; begin Ada.Text_IO.Put_Line ( "The total is: " & Number'Image(j) ); j := Next (j); end Print_and_Increment; -- package initialization executed when the package is elaborated begin while i < Number'Last loop Print_and_Increment (i); end loop; end Example; </syntaxhighlight> This program can be compiled, e.g., by using the freely available open-source compiler [[GNAT]], by executing <syntaxhighlight lang="bash">gnatmake -z example.adb</syntaxhighlight> Packages, procedures and functions can nest to any depth, and each can also be the logical outermost block. Each package, procedure or function can have its own declarations of constants, types, variables, and other procedures, functions and packages, which can be declared in any order.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)