This chapter shows a few simple programs to give the 'feel' of an Ada program.
One of Ada's major philosophies is encapsulation. All of the standard I/O routines come presupplied in packages that can be included in a program. The following examples use the text_io package.
with Ada.Text_IO; use Ada.Text_IO; -- a package containing the"put_line" procedure procedure hello is -- candidate for the "main" procedure. begin put_line("hello"); end;
Unlike C which has a function main, and Pascal which has a program, any parameterless procedure can be a "main" routine. The procedure thus designated is chosen at link time.
with Ada.Text_IO; use Ada.Text_IO; with Hello; -- include our previous procedure procedure your_name is name :string(1..100); -- 100 character array last :natural; -- can only contain natural integers begin put("Hello what is your name? "); get_line(name,last); for i in 1..10 loop -- i is implicity declared Hello; put_line(" there " & name(1..last)); -- & string concatenation -- name(1..last)- array slice end loop; -- control structure labelled end;
Inputting numbers requires the use of a package devoted to the task. A package to do this exists with all Ada implementations. Other simpler packages are often created within a site (see package simple_io in the appendices).
with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO procedure Age is Age :integer range 0..120; begin Put("hello, how old are you ? "); Get(Age); -- might cause an exception if value entered --is outside range if Age < 18 then put_line("ha! you're just a baby"); elsif Age < 60 then -- note spelling of elsif put_line("working hard?"); else put_line("Now take it easy old fella!"); end if; exception when constraint_error => put_line("sorry only ages 0..120 are accepted"); end age; -- procedure name is optional at end;