3.10 The return Statement

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

The return statement is a simple statement used to complete execution of a subprogram,  returning control to the calling unit. There are two forms: one for functions and one for procedures. Both forms use the reserved word, return.

General Form for a function

General Form for a procedure

return Expression; 
return;

Every function must have at least one return statement. A procedure is not required to have any return statements. The expression in a function's return statement must evaluate to a type that matches the return type in the function's declaration.

Example program illustrating both types of return statement.

Im3-11.gif (3038 bytes)

In the program depicted here the function, Max_Of_Four, has four return statements, and the procedure, Show_Winners, has one return statement.

The test procedure steps through two cases, the first having a single winner and the second having a tie where the top three scores are equal.

Note that in the case of the Show_Winners procedure, execution may terminate as a result of reaching the return statement in some cases, and as a result of reaching the end of the executable part in other cases.

Source Code Listing

------------------------ Max_Of_Four --------------------------
--  This function selects the largest of four integers. 
--  It has four return statements.
---------------------------------------------------------------
function Max_Of_Four(A,B,C,D : Integer) return Integer is   
begin
  if (A >= B) and (A >= C) and (A >= D) then
      return A;
  elsif (B >= C) and (B >= D) then
      return B;
  elsif (C >= D) then
      return C;
  else 
      return D;
  end if;           
end Max_Of_Four;
---------------------------------------------------------------      
------------------------ Show_Winners -------------------------
--  This procedure displays the names of winners and the number 
--  of winners if there is a tie. It has one return statement. 
--------------------------------------------------------------- 
with Max_Of_Four;
with Ada.Text_IO;
use  Ada.Text_IO;
procedure Show_Winners(A,B,C,D : in Integer) is
    Count : Integer := 0;
begin
  if A = Max_Of_Four(A,B,C,D) then
      Count := Count + 1;
      Put_Line("Adam is a winner.");
  end if;
  
  if B = Max_Of_Four(A,B,C,D) then
      Count := Count + 1;
      Put_Line("Bill is a winner.");
  end if;
  
  if C = Max_Of_Four(A,B,C,D) then
      Count := Count + 1;
      Put_Line("Carl is a winner.");
  end if;
  
  if D = Max_Of_Four(A,B,C,D) then
      Count := Count + 1;
      Put_Line("Dave is a winner.");
  end if;
  
  if Count = 1 then
      Put_Line("There is only one winner.");
      return;                               -- return statement
  end if;
  
  Put_Line("There are " & Integer'Image(Count) 
                        & " Winners");                    
         
end Show_Winners;
---------------------------------------------------------------
----------------------- Test_Winners --------------------------
--  This test procedure creates two cases: one with a single 
--  winner and one with a three-way tie.   
---------------------------------------------------------------
with Show_Winners;
with Ada.Text_IO;
use  Ada.Text_IO;
procedure Test_Winners is
begin
  Put_Line("  First Case");
  Show_Winners(8, 12, 18, 13);
  New_Line;
  Put_Line("  Second Case");
  Show_Winners(20, 15, 20, 20);  
end Test_Winners;
---------------------------------------------------------------

Note that the final statement of procedure Show_Winners uses the Image attribute (see discussion in Chapter 4 and list in Appendix A).

The above program produces the following output:

    First Case
  Carl is a winner.
  There is only one winner.

    Second Case
  Adam is a winner.
  Carl is a winner.
  Dave is a winner.
  There are 3 winners.

Related Topics

2.4 Procedures 2.5 Functions
A.2 Simple and Compound Statements A.3 List of Attributes

[ Back to top of pagePrev ] Next ]