Although Ada doesn't officially have in out parameters for functions,
you can still give a function behavior that amounts to the same thing.
One useful thing to be able to do is to modify the item on top of the
stack, without actually copying a new item to the top. This doesn't
mean replacing the item on the top with another item that has a
different value -- it means modifying the actual item, in place.
What we do is to return a reference to the top item. Ada doesn't have
reference types like you have in C++, so we use access types instead,
which give you the same thing with only a small amount of additional
syntactic overhead.
The operation Set_Top is _function_ that allows the client to modify the
item on top of the stack, by returning a pointer to the item on top:
type Item_Access is
access all Item_Type;
function Set_Top
(Stack : Root_Stack)
return Item_Access is abstract;
You use it as follows:
Set_Top (Stack).all := ;
or, if the Item_Type is a record:
Set_Top (Stack). := ;
or, passing it to a modifier:
Do_Something (To_The_Object => Set_Top (Stack).all);
For example, suppose a stack of integers has the following value (top to
bottom):
3 2 1
If I say
Set_Top (Stack).all := 4;
then the stack now looks like
4 2 1
|