Thursday,
February 21 Week 6
Chapter 5
Student Notes
Objectives
the Math library
Writing functions
Writing Packages
1. mathematical functions in
·
·
Ada.Numerics.Elementary_Fuctions.Sqrt(X
=> SomeFloat);
·
The name of SQRT’s float parameter is X
2. Writing
function declarations
·
In general, a function is written so as to require the caller to supply
some values to it. When called, the function performs its desired computation
and then a result to the calling program.
·
The line indicating the name of the function, the expected parameters,
and the returned are called the or
·
Here is one you have seen before in chapter 4:
FUNCTION Year (date :
time) RETURN Year_Number;
We could also declare
FUNCTION Maximum (Value1, Value2 : Integer) RETURN Integer;
3. Calling a
function
·
·
·
ThisYear
: Ada.Calendar.Year_Number;
RightNow : Ada.Calendar.Time;
If we declare these two
variables, we can then call the function Year inside our program like this:
ThisYear := Ada.Calendar.Year(Date =>
RightNow);
After executing this
function, ThisYear
would hold the Year_Number of RightNow.
4. Writing
function bodies
·
·
·
·
Function
Maximum (value1, Value2 : Integer) Return Integer IS
Result : Integer;
BEGIN
IF value1 > Value2 THEN
Result := Value1;
ELSE
Result := value2;
END IF;
RETURN Result;
END Maximum;
5. Using
functions in an executable program
·
Functions can occur in two places.
·
EX. program 5.5 Max_Two
·
Function spec before BEGIN and after procedure variable declarations
·
Then function body
·
Then begin for the executable procedure Max_Two
·
Calling the function within the executable procedure
6. Writing a
package
·
We will often deal with functions that have been encapsulated into
reusable packages.
·
The spec contains the function
declarations
·
the package body contain the function bodies