[Santa Clara University]
Department of Mathematics
and Computer Science

Math 169

Ada/Pascal Comparison


OVERVIEW and COMPARISONS
between PASCAL and Ada

An Ada program is (can be) a collection of separately compilable sections called packages or tasks. Tasks are language units used for parallel (concurrent) program sections. Otherwise packages and tasks are structurally similar. Each package begins with the header line PACKAGE BODY IS and ends with the reserved word END, followed by the followed by a semi-colon.

The main program is itself a procedure (or a package), with a general structure as follows:

	PROCEDURE an_example IS
	--type and variable declarations
	BEGIN
		...
	END an_example;
Each package can have its own PROCEDUREs and there is no need to have more than one package.

Multiple packages can be important in large software-engineering type projects where separate groups/individuals are working on separate segments of a program code. Each package can be protected against unauthorized changes in its data structures through encapsulation. In addition, good code can be compiled once and not have to be re-compiled every time a small change is being made in another section.

Packages can consist of two parts: the visible specification section and the implementation body. This enables a program to encapsulate data and prevent a user from directly (and incorrectly) accessing the underlying data structure. In the header for a specification part, the reserved word BODY is omitted.

To "borrow" procedures from other packages, WITH and USE clauses are used before the package or procedure header. For example, to use the standard i-o procedures (for screen and terminal i-o) in the standard i-o library, one should have two clauses before the program:

	WITH TEXT_IO;
	USE TEXT_IO;
The WITH clause accesses the package with the necessary (library) procedures. The USE clause is similar to the WITH clause in Pascal, eliminating the necessity of prefixing the package name before procedures borrowed from that package. For example, "Put" is an output function in Package TEXT_IO. Without the USE TEXT_IO clause, one would have to use TEXT_IO.PUT in the code, whereas with the USE TEXT_IO clause, one merely uses PUT in the code.

Packages can also be designated as GENERIC. This is similar to defining a new data type (e.g. in Pascal). The actual package is "instantiated" in various points in later code with specification of (variables or other) elements which may be changeable. For example, one need only define a "StackModule" package once, without specifying the type of elements to be put on the stack. After writing the generic code for the package (as a general "template" of what that type of code should be), when you need a certain type of stack, you can "instantiate" at the proper point of the program, e.g.,

         PACKAGE RealStack IS NEW StackModule(FLOAT)
	 PACKAGE IntStack IS NEW StackModule(INTEGER)
Then one can use RealStack or IntStack as actually instantiated packages and use the PUSH and POP procedures on the proper type of elements.

BEGINs are used less frequently than in Pascal, normally only at the beginning of a PROCEDURE or PACKAGE. The corresponding END at the END of a PROCEDURE or PACKAGE must be followed with the name of the PROCEDURE or PACKAGE, e.g. END MainProgram.

TYPEs of variables are FLOAT, INTEGER, BOOLEAN, CHARACTER, STRING (enclosed in DOUBLE quotes). Variables are DECLARED as in Pascal, with NAMES (separated by commas), a COLON, and then the TYPE. The Pascal reserved word VAR, introducing the declarations, is NOT used in Ada.

Ada DOES include the exponentiation operator, ** .

Some structural differences/analogies:

--------------------------------|---------------------------------
	PASCAL				ADA
--------------------------------|---------------------------------
	WHILE ... DO			WHILE ... LOOP
	   BEGIN
		...			   ...
	   END;				END LOOP;
--------------------------------|---------------------------------
	FOR i := 1 TO 100 DO		FOR i IN 1..100 LOOP
	   BEGIN
		...                        ...
	   END;				END LOOP;
--------------------------------|---------------------------------
	FOR i := 100 DOWNTO 1 DO	FOR i IN REVERSE 1..100 LOOP
	   BEGIN
		...			   ...
	   END;			        END LOOP;
--------------------------------|---------------------------------
	IF ... THEN			IF ... THEN
	   BEGIN
		...		           ...
	   END
	ELSE				ELSE
	   BEGIN
		...		           ...
	   END;				END IF;

				  A nested IF can be introduced
				  by an ELSIF ... THEN instead
				  of the ELSE line.
--------------------------------|---------------------------------
	CASE i OF			CASE i IS
	   a: action1;		           WHEN a  =>  action1;
	   b: BEGIN			   WHEN b  =>  action2a;
	        action2a;			        action2b;
 	        action2b;
	      END;
	   c,d,e: action3;		   WHEN c | d | e => action3;
	END;				   WHEN OTHERS  => NULL;
					END CASE;
--------------------------------|---------------------------------
NOTE: The variable used in an ADA FOR statement is considered local to the loop and need not be declared outside of the loop.

As with Modula-2, all structured statements end with an explicit ending reserved word combination: usually it is END followed by the construct title. There is no BEGIN. For example:

	IF i > 0 THEN
		Put("Truth");
		i := -1;
	END IF;
	WHILE j>3 LOOP
		j:=j+i;
	END LOOP;
Thus, the IF, WHILE, FOR statements MUST end with an END, even if the action is ONLY ONE STATEMENT.

The CASE statement may have an WHEN OTHERS clause. Actions which are more than one statement long are permitted (i.e. no BEGIN and END if an option contains more than one statement). Multiple cases grouped together are each separated by a vertical bar (|).

As in Modula-2, in the IF statement, a nested IF can be introduced with the reserved word ELSIF. No BEGIN..END pairs are necessary between the THEN and the ELSE or ELSIF and the statement concludes with an END IF.

Ada also has a generalized LOOP statement, although it does not have the REPEAT statement of Pascal and Modula-2. The LOOP statement is a generalized repetition structure which allows mid-loop existing via an EXIT statement. It ends with an END LOOP statement, and somewhere inside the loop, there should be an (one or more) EXIT statement(s) (frequently the action of an IF statement). The EXIT statement can be followed with a WHEN clause and thus the LOOP can imitate a REPEAT. For example,

           LOOP
		...
	        EXIT WHEN a < b;
	   END LOOP;
All LOOPS (FOR, WHILE, Generalized) can be labeled. An identifier can precede the loop (separated by a COLON). If a label is used, the label can appear in EXIT statements, and at the end (e.g. END LOOP LOOP1A).

Comments are introduced by a double hyphen (--). Comments go to the end of the line and have no concluding symbol.

When defining a pointer type, one uses the reserved words IS ACCESS instead of the preceding up-arrow as in Pascal. For example, TYPE PTR IS ACCESS Node; The equal sign is NOT used in defining new types in Ada; the reserved word IS is used instead.

Strings are enclosed in DOUBLE quotes.

Identifiers must start with a letter of the alphabet and can use the underline (_) as a character (but NOT at the beginning or end, or two in a row). Identifiers must not be longer than 1 physical line.

Defining a user function in Ada is similar to what happens in Pascal. The TYPE of the FUNCTION must appear in the header and is preceded by the word RETURN.

E.g.,

	FUNCTION Signum (Freud: INTEGER) RETURN INTEGER IS
As in Modula-2, instead of assigning the function name a value, one returns a value via the RETURN statement, e.g.
	IF Freud < 0 THEN
		RETURN(-1)
	ELSE
		RETURN(0)
	END IF;
Not-equals in boolean relational expressions is expressed by a divide-slash and equals sign, /=.

In procedures and functions, Ada provides three modes for formal parameters. The mode IN indicates that the parameter is call by value. The mode OUT indicates that the parameter is a local variable and receives no value from the calling segment, but passes a value back upon termination of the routine. The mode IN OUT is a combination of the other two in result, but the exact parameter passing scheme depends on the implementation and the type of parameter. For scalar and access type (pointers) parameters, call by value-result is used. For structured parameters, the languages does not define the mechanism and either call by value-result or call by reference can be used.

The concept of an IN parameter in Ada differs from that of a value parameter in Pascal. The IN parameter of an Ada function or procedure is NOT a variable, but a name for a value. As a consequence, an IN parameter is considered a constant in the body of a function or procedure, and, therefore, one cannot assign to an IN parameter. Functions may only have IN parameters. If the mode is omitted, it is assumed to be IN.

I-O procedures in Ada must be imported from the appropriate packages. Terminal i-o routines are GET and PUT, and are in package TEXT_IO. File i-o routines are READ and WRITE, and are in package SEQ_IO. (In addition, files need to be OPENed and CLOSEd with appropriate procedures.)


My email address is dsmolarski@math.scu.edu

This page last updated 16 March 2000.