CSc 520 - Principles of Programming Languages
27 : Control Structures -- Procedures

Christian Collberg

Department of Computer Science

University of Arizona

1 Procedures as Control Abstractions

2 Procedures as Control Abstractions...

3 Questions


Case Study -- Pascal


4 Pascal Procedures


1#1

5 Pascal Procedures...


2#2

6 Pascal Procedures...

7 Pascal Procedures...

8 Pascal Procedures...


Case Study -- Ada


9 Ada -- Subprogram Declarations

procedure Traverse_Tree;
procedure Increment(X : in out Integer);
procedure Right_Indent(Margin : out Line_Size);          
procedure Switch(From, To : in out Link);                

function Random return Probability;                      

function Min_Cell(X : Link) return Cell;                 
function Next_Frame(K : Positive) return Frame;          
function Dot_Product(Left, Right : Vector) 
                       return Real;

10 Ada -- Subprogram Declarations

function "*"(Left, Right : Matrix) return Matrix;
procedure Print_Header(Pages  : in Natural;
          Header : in Line    :=  
                    (1 .. Line'Last => ' ');  
          Center : in Boolean := True);

11 Ada -- Subprogram Bodies

-- Example of procedure body:

procedure Push(E : in Element_Type; 
               S : in out Stack) is
begin
   if S.Index = S.Size then
      raise Stack_Overflow;
   else
      S.Index := S.Index + 1;
      S.Space(S.Index) := E;
   end if;
end Push;

12 Ada -- Procedure Call

Traverse_Tree;                                               
Print_Header(128, Title, True);                              

Switch(From => X, To => Next);                               
Print_Header(128, Header => Title, 
             Center => True);          
Print_Header(Header=>Title, 
             Center=>True, Pages=>128); 

--Examples of function calls:
Dot_Product(U, V)   
Clock

13 Ada -- Procedure Call

-- Procedures with default expressions:
procedure Activate(
      Process : in Process_Name;
      After   : in Process_Name:=No_Process;
      Wait    : in Duration := 0.0;
      Prior   : in Boolean := False);

procedure Pair(Left, Right : 
                  in Person_Name:=new Person);

14 Ada -- Procedure Call...

-- Examples of their calls:
Activate(X);
Activate(X, After => Y);
Activate(X, Wait => 60.0, Prior => True);
Activate(X, Y, 10.0, False);
Pair;
Pair(Left => new Person, Right => new Person);

15 Ada -- Overloaded Calls

procedure Put(X : in Integer);
procedure Put(X : in String);

procedure Set(Tint   : in Color);
procedure Set(Signal : in Light);

-- Examples of their calls:
Put(28);
Put("no possible ambiguity here");

Set(Tint=>Red);   --  Set(Red) is ambiguous. 
Set(Signal=>Red); --  Red can denote either 
Set(Color'(Red)); --  a Color or a Light

16 Ada -- Userdefined Operators

function "+" (Left,Right:Matrix) return Matrix;
function "+" (Left,Right:Vector) return Vector;

-- assuming that A, B, and C are of 
-- the type Vector the following two 
-- statements are equivalent:

A := B + C;
A := "+"(B, C);

17 Readings and References

18 Summary

19 Summary...



Christian Collberg 2008-03-31