C.1. Parameters - JulTob/Ada GitHub Wiki
Parameter Modes
Parameter Mode | Explanation |
---|---|
in |
Read Only, treated as constant in the process. Default when omitted. |
out |
Assigns a value to it in the output environment. Uninitialized. |
in out |
Reads to Initialize the parameter, but can be overwritten with new values in the environment. |
Default value
When omitted the value, it will take the default value.
procedure Example
( X: Integer;
Y: Integer := 0;
Z: Integer := 1);
Calling
You can call a process in different ways:
Example (1,2,3); --Ordered X,Y,Z
Example (1); --Will take default values
Example ( Z=> 2, X => 1 , Y => 0); --Named parameter association
--Non working example of a procedure syntaxis
procedure Mode_Example (X : in Integer:=0; --Read only parameter with default value
Y : out Integer; --Write only var
Z : in out Integer) is
begin
X := 1;
-- Error. Can’t modify an in parameter.
Y := X;
-- Okay. Can read X and modify Y.
Z := 1;
-- Always initialize.
Z := Z + 1;
-- Okay. Can read and write an in out parameter.
end Mode_Example;
--Named association:
Mode_Example(Z => Accumulator, X => N+15, Y => Output);
in
This is the default, meaning if you do not specify an operator in
is assumed.
When you put it, the method will make a copy of the object. This is useful when you really do not want the original value to be modified. However, the flip side of this is that if the passed in object is very large, then the copy will be very time- and memory consuming. At first, this performance penalty might not be very obvious, but if called often enough, performance will be impacted.
Also, you cannot assign a value to a
variable passed to a function/procedure by in
. This will get you
a compile-time error.
out
In this case, the actual value of the variable going in does not matter.
You will need to assign a value to this variable once inside of the function/parameter (not doing this will make the compiler complain).
Think of it this way, out
acts as if you have a new variable created for you in the method, it returns the value assigned to the caller.
in out
The benefit of this is that you can now pass in values based on reference.
With pass by reference, what is being passed in is the reference value, not the whole variable.
Unlike in
, there is no performance hit.
This approach is highly recommended over the return if you are working with very large strings or very large data types.
When using in out
, you must pass in a variable as
opposed to a static value (passing in a static value will get you acompile-time error).