[ Table of Contents ]
The following table depicts the general form of a function. Note that five reserved words (function, return, is, begin, end) are always included. Every function has a name, a return type, and an executable part. The executable part must always include at least one return statement. There may also be an exception handling part preceded by the reserved word exception.
General Form of a function body |
function Function_Name(formal parameters) return (Return_Type) is -- declarative part (optional) begin -- executable part (normal actions) exception -- (optional) -- exception handling actions (optional) end Funtion_Name; |
Example Program with a Function and a Procedure
This diagram depicts the architecture of the program listed below. The
main (test) procedure depends on the To_Fahrenheit function and on two packages from the
predefined environment. The To_Fahrenheit function has no dependencies. All four modules
are library units. Rather than draw two Ada package symbols, we have simply listed their names inside a UML package symbol -- a graphical shorthand we will use repeatedly in later examples. |
The top part of the next table lists the To_Fahrenheit function , which takes a floating point Celsius value and returns the corresponding Fahrenheit value. A procedure called Test_To_Fahrenheit is listed in the bottom part of the table.
Example function | function To_Fahrenheit (C : Float) return Float is Base : constant Float := 32.0; Ratio : constant Float := 1.8; begin return (Base + Ratio*C): end To_Fahrenheit; |
A procedure to call the example function | with To_Fahrenheit; with Ada.Text_IO, Ada.Float_Text_IO; procedure Test_To_Fahrenheit is package Tio renames Ada.Text_IO; package Fio renames Ada.Float_Text_IO; C_Hot : constant Float := 30.0; C_Nice : constant Float := 20.0; C_Chilly : constant Float := 10.0; C_Ice : constant Float := 0.0; F_Value : Float; begin F_Value := To_Fahrenheit(C_Hot); Tio.Put("A hot Fahrenheit value is "); Fio.Put(F_Value); Tio.New_Line; F_Value := To_Fahrenheit(C_Nice); Tio.Put("A nice Fahrenheit value is "); Fio.Put(F_Value); Tio.New_Line; end Test_To_Fahrenheit; |
Two different procedures named Put are used above. One is exported by Ada.Text_IO, and the other is exported by Ada.Float_Text_IO. Note the calls to the New_Line procedure following each pair of calls to Put.
Note that the actual parameters used to call the To_Fahrenheit function are constant values. If you compile, link and run the above function and procedure, using Test_To_Fahrenheit as the main procedure, the program should display the following lines of text:
A hot Fahrenheit value is 8.60000E+01 A nice Fahrenheit value is 6.80000E+01 |
We could have added eight more lines to the procedure, using the same pattern, to produce two additional lines of output:
A chilly Fahrenheit value is 5.0000E+01 An icy Fahrenheit value is 3.20000E+01 |
Notes about the general form and the example:
As indicated above, there are two optional parts of a function body: the declarative part and the exception
handling part. The example includes a declarative part, but no exception handler.
It is mandatory that every function body start with the reserved word function followed by the name of the function. Following the (optional) parameter list, there must be the reserved word return and the name of the type of the returned value.
It is also mandatory that every function body include an executable part starting with the word begin, ending with end; and containing at least one executable statement between them. The sequence of executable statements must include at least one return statement.
Parameter Profile for a Function
The parameter profile of a function is similar to that of a procedure, except that all parameters must be of mode in and the specification of the type of the returned value is also considered to be part of the profile. Since all parameters are of mode in, it is not necessary to include the word in as part of the declaration.
Related Topics
2.2 Program Units | 3.3 Function Calls | 3.10 The return Statement |
[ Back to top of page ]