C. Processes: Procedures and Functions - JulTob/Ada GitHub Wiki
Processes
Procedures
Processes
. Reflects into actions when called.
Functions
Processes that produce data
. It returns a result when called.
Methods
Another name for processes is methods
. Especially if related to a class, or structures, and called from an element.
element.method
Method only applies if it belongs to something, and is a method of.
This lexicon is only to diferentiate that it belongs to a context. It may be that append
a method of text, withdraw
a method of banking, or pop
a method of the database.
Methods don't exist in a vacuum.
Processes can be declared in any declarative part.
procedure Say_Hi is
Hi : string := "Hi!";
begin
put(Hi);
end Say_Hi;
function Double(I: Integer) is
begin
return 2*I;
end Double;
Procedures and Functions can be overloaded, and they will be chosen depending on the parameters’ types or libraries on the scope.
Notice that Ada does not require (or even allow) you to use an empty parameter list on functions that take no parameters. This means that function End_Of_File looks like a variable when it is used. This is considered a feature; it means that read-only variables can be replaced by parameterless functions without requiring any modification of the source code that uses that variable.
return
Return is used to determine the value a function gives back, finishing execution, or to halt a procedure.
Note both cases terminate the process.
procedure Send_Text(S: String) is
begin
if S = "" then return; end if;
-- Avoid sending an empty String;
...
; – Now, it is true that you cannot return a value from a procedure, but that does not mean that you cannot return from one without a value. What this does is that it simply goes back to the caller. You do not need an explicit call of this nature at the end of the procedure, but if you have several if statements and want to return after a specific condition is met, then this is how you would do it
Procedure calling
La llamada a un procedimiento especifica la relación entre los parámetros reales y los formales y ejecuta el procedimiento. Los parámetros se asocian normalmente por posición, aunque, opcionalmente, también se pueden asociar por nombre. Si el procedimiento tiene parámetros formales por omisión, no es necesario asociarles un parámetro real.
procedure Ejemplo (A, B: in Integer; C: out Float; D : in out Character);
...
X, Y : Integer;
C : Character;
R : Float;
...
Ejemplo (X, Y, R, C);
Ejemplo (X, 12 * 3, R, C);
Ejemplo (A => X, B => Y, R, C);