From Clomosy Docs

(Created page with "Procedures are subprograms that, instead of returning a single value, allow to obtain a group of results.<br> In TRObject, a procedure is defined using the void keyword in TRObject syntax, and the procedure keyword in Base syntax. The general format of a procedure definition is as follows.<br> <div class="row"> <div class="col-lg-6"> <div class="card"> <p> <b>TRObject Syntax</b></p> <pre> void name(argument(s): type1, argument(s): type 2, ... ); < local declarations...")
 
No edit summary
Line 78: Line 78:
<b>TRObject Syntax</b><br>
<b>TRObject Syntax</b><br>
<pre>
<pre>
var
  mValue : Integer;
void calMin(x, y, z: integer; var m: integer);
{
  if (x < y)
      m = x
  else
      m = y;
 
  if (z < m)
      m = z;
}
{
  mValue = 5;
  calMin(2,3,4,mValue);
  ShowMessage('Minimum: '+ IntToStr(mValue));
}
</pre>
</pre>
<b>Base Syntax</b><br>
<b>Base Syntax</b><br>
<pre>
<pre>
var
  mValue : Integer;
procedure calMin(x, y, z: integer; var m: integer);
begin
  if x < y then
      m:= x
  else
      m:= y;
 
  if z < m then
      m:= z;
end;
begin
  mValue := 5;
  calMin(2,3,4,mValue);
  ShowMessage('Minimum: '+ IntToStr(mValue));
end;
</pre>
</pre>
When the above code is compiled and executed, it produces the following result:<br>
<div class="alert alert-secondary" role="alert" data-bs-theme="light">
&nbsp;Minimum: 2
</div>

Revision as of 08:12, 4 October 2024

Procedures are subprograms that, instead of returning a single value, allow to obtain a group of results.

In TRObject, a procedure is defined using the void keyword in TRObject syntax, and the procedure keyword in Base syntax. The general format of a procedure definition is as follows.

TRObject Syntax

void name(argument(s): type1, argument(s): type 2, ... );
   < local declarations >
{
   < procedure body >
}

Base Syntax

procedure name(argument(s): type1, argument(s): type 2, ... );
   < local declarations >
begin
   < procedure body >
end;


Here are all the parts of a procedure:

  • Arguments − The argument(s) establish the linkage between the calling program and the procedure identifiers and also called the formal parameters. Rules for arguments in procedures are same as that for the functions.
  • Local declarations − Local declarations refer to the declarations for labels, constants, variables, functions and procedures, which are applicable to the body of the procedure only.
  • Procedure Body − The procedure body contains a collection of statements that define what the procedure does. It should always be enclosed between the reserved words begin and end. It is the part of a procedure where all computations are done.

Procedure Declarations

A procedure declaration tells the compiler about a procedure name and how to call the procedure. The actual body of the procedure can be defined separately.

A procedure declaration has the following syntax:


TRObject Syntax

Base Syntax

Please note that the name of the procedure is not associated with any type. For the above defined procedure clMin(), following is the declaration:

TRObject Syntax

Base Syntax

Calling a Procedure

While creating a procedure, you give a definition of what the procedure has to do. To use the procedure, you will have to call that procedure to perform the defined task. When a program calls a procedure, program control is transferred to the called procedure. A called procedure performs the defined task, and when its last end statement is reached, it returns the control back to the calling program.

To call a procedure, you simply need to pass the required parameters along with the procedure name as shown below:

Example
TRObject Syntax

var
  mValue : Integer;
void calMin(x, y, z: integer; var m: integer);
{
   if (x < y)
      m = x
   else
      m = y;
   
   if (z < m)
      m = z;
}

{
  mValue = 5;
  calMin(2,3,4,mValue);
  ShowMessage('Minimum: '+ IntToStr(mValue));
}

Base Syntax

var
  mValue : Integer;
procedure calMin(x, y, z: integer; var m: integer);
begin
   if x < y then
      m:= x
   else
      m:= y;
   
   if z < m then
      m:= z;
end;

begin
  mValue := 5;
  calMin(2,3,4,mValue);
  ShowMessage('Minimum: '+ IntToStr(mValue));
end;

When the above code is compiled and executed, it produces the following result: