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!
== Language constructs == Ada is an [[ALGOL]]-like programming language featuring control structures with reserved words such as ''if'', ''then'', ''else'', ''while'', ''for'', and so on. However, Ada also has many data structuring facilities and other abstractions which were not included in the original [[ALGOL 60]], such as [[type system|type definitions]], [[record (computer science)|records]], [[pointer (computer programming)|pointers]], [[enumerated type|enumerations]]. Such constructs were in part inherited from or inspired by [[Pascal (programming language)|Pascal]]. === "Hello, world!" in Ada === A common example of a language's [[Syntax (programming languages)|syntax]] is the [["Hello, World!" program]]: (hello.adb) <syntaxhighlight lang="ada" line> with Ada.Text_IO; procedure Hello is begin Ada.Text_IO.Put_Line ("Hello, world!"); end Hello; </syntaxhighlight> This program can be compiled by using the freely available open source compiler [[GNAT]], by executing <syntaxhighlight lang="bash">gnatmake hello.adb</syntaxhighlight> === Data types === Ada's type system is not based on a set of predefined [[primitive types]] but allows users to declare their own types. This declaration in turn is not based on the internal representation of the type but on describing the goal which should be achieved. This allows the compiler to determine a suitable memory size for the type, and to check for violations of the type definition at compile time and run time (i.e., range violations, buffer overruns, type consistency, etc.). Ada supports numerical types defined by a range, modulo types, aggregate types (records and arrays), and enumeration types. Access types define a reference to an instance of a specified type; untyped pointers are not permitted. Special types provided by the language are task types and protected types. For example, a date might be represented as: <syntaxhighlight lang="ada" line> type Day_type is range 1 .. 31; type Month_type is range 1 .. 12; type Year_type is range 1800 .. 2100; type Hours is mod 24; type Weekday is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); type Date is record Day : Day_type; Month : Month_type; Year : Year_type; end record; </syntaxhighlight> Important to note: Day_type, Month_type, Year_type, Hours are incompatible types, meaning that for instance the following expression is illegal: <syntaxhighlight lang="ada" line> Today: Day_type := 4; Current_Month: Month_type := 10; ... Today + Current_Month ... -- illegal </syntaxhighlight> The predefined plus-operator can only add values of the same type, so the expression is illegal. Types can be refined by declaring [[subtyping|subtypes]]: <syntaxhighlight lang="ada" line> subtype Working_Hours is Hours range 0 .. 12; -- at most 12 Hours to work a day subtype Working_Day is Weekday range Monday .. Friday; -- Days to work Work_Load: constant array(Working_Day) of Working_Hours -- implicit type declaration := (Friday => 6, Monday => 4, others => 10); -- lookup table for working hours with initialization </syntaxhighlight> Types can have modifiers such as ''limited, abstract, private'' etc. Private types do not show their inner structure; objects of limited types cannot be copied.<ref>{{cite web |title=Ada Syntax Card |url=http://www.digilife.be/quickreferences/QRC/Ada%20Syntax%20Card.pdf |access-date=28 February 2011 |archive-url=https://web.archive.org/web/20110706133825/http://www.digilife.be/quickreferences/QRC/Ada%20Syntax%20Card.pdf |archive-date=6 July 2011}}</ref> Ada 95 adds further features for object-oriented extension of types. === Control structures === Ada is a [[structured programming]] language, meaning that the flow of control is structured into standard statements. All standard constructs and deep-level early exit are supported, so the use of the also supported "[[goto (command)|go to]]" commands is seldom needed. <syntaxhighlight lang="ada" line> -- while a is not equal to b, loop. while a /= b loop Ada.Text_IO.Put_Line ("Waiting"); end loop; if a > b then Ada.Text_IO.Put_Line ("Condition met"); else Ada.Text_IO.Put_Line ("Condition not met"); end if; for i in 1 .. 10 loop Ada.Text_IO.Put ("Iteration: "); Ada.Text_IO.Put (i); Ada.Text_IO.Put_Line; end loop; loop a := a + 1; exit when a = 10; end loop; case i is when 0 => Ada.Text_IO.Put ("zero"); when 1 => Ada.Text_IO.Put ("one"); when 2 => Ada.Text_IO.Put ("two"); -- case statements have to cover all possible cases: when others => Ada.Text_IO.Put ("none of the above"); end case; for aWeekday in Weekday'Range loop -- loop over an enumeration Put_Line ( Weekday'Image(aWeekday) ); -- output string representation of an enumeration if aWeekday in Working_Day then -- check of a subtype of an enumeration Put_Line ( " to work for " & Working_Hours'Image (Work_Load(aWeekday)) ); -- access into a lookup table end if; end loop; </syntaxhighlight> === 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. === Pragmas === A pragma is a [[compiler directive]] that conveys information to the compiler to allow specific manipulating of compiled output.<ref>{{cite web |url=http://archive.adaic.com/standards/83lrm/html/lrm-02-08.html#2.8 |title=Ada 83 LRM, Sec 2.8: Pragmas |publisher=Archive.adaic.com |access-date=2014-01-27}}</ref> Certain pragmas are built into the language,<ref>{{cite web |url=http://archive.adaic.com/standards/83lrm/html/lrm-B.html |title=Ada 83 LRM, Appendix/Annex B: Predefined Language Pragmas |publisher=Archive.adaic.com |access-date=2014-01-27 |archive-url=https://web.archive.org/web/20120206005541/http://archive.adaic.com/standards/83lrm/html/lrm-B.html |archive-date=2012-02-06}}</ref> while others are implementation-specific. Examples of common usage of compiler pragmas would be to disable certain features, such as run-time type checking or array subscript boundary checking, or to instruct the compiler to insert object code instead of a function call (as C/C++ does with [[inline function]]s). === Generics === {{excerpt|Generic programming|Generics in Ada}}
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)