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
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
Procedures are subprograms that, instead of returning a single value, allow to obtain a group of results.<br>
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>
A procedure is defined using the void keyword. 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>
<pre>
void name(argument(s): type1, argument(s): type 2, ... );
void name(argument(s): type1, argument(s): type 2, ... );
Line 14: Line 10:
}
}
</pre>
</pre>
</div>
</div>
<div class="col-lg-6">
<div class="card">
<p> <b>Base Syntax</b></p>
<pre>
procedure name(argument(s): type1, argument(s): type 2, ... );
  < local declarations >
begin
  < procedure body >
end;
</pre>
</div>
</div>
</div>
</div>
<br>
<br>


Line 39: Line 18:
* <b>Local declarations</b> − Local declarations refer to the declarations for labels, constants, variables, functions and procedures, which are applicable to the body of the procedure only.
* <b>Local declarations</b> − Local declarations refer to the declarations for labels, constants, variables, functions and procedures, which are applicable to the body of the procedure only.


* <b>Procedure Body</b> − 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.
* <b>Procedure Body</b> − The procedure body contains a collection of statements that define what the procedure does. It should always be enclosed between the reserved words start ( { ) and end ( } ). It is the part of a procedure where all computations are done.


<h2> Procedure Declarations </h2>
<h2> Procedure Declarations </h2>
Line 46: Line 25:
A procedure declaration has the following syntax:<br>
A procedure declaration has the following syntax:<br>


<b>TRObject Syntax</b><br>
<div class="alert alert-secondary" role="alert" data-bs-theme="light">
<div class="alert alert-secondary" role="alert" data-bs-theme="light">
&nbsp;void name(argument(s): type1, argument(s): type 2, ... );
&nbsp;void name(argument(s): type1, argument(s): type 2, ... );
</div>
<b>Base Syntax</b><br>
<div class="alert alert-secondary" role="alert" data-bs-theme="light">
&nbsp;procedure name(argument(s): type1, argument(s): type 2, ... );
</div>
</div>


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


<b>TRObject Syntax</b><br>
<div class="alert alert-secondary" role="alert" data-bs-theme="light">
<div class="alert alert-secondary" role="alert" data-bs-theme="light">
&nbsp;void calMin(x, y, z: integer; var m: integer);
&nbsp;void calMin(x, y, z: integer; var m: integer);
</div>
</div>


<b>Base Syntax</b><br>
<div class="alert alert-secondary" role="alert" data-bs-theme="light">
&nbsp;procedure calMin(x, y, z: integer; var m: integer);
</div>


<h2> Calling a Procedure </h2>
<h2> Calling a Procedure </h2>
Line 76: Line 43:


<b>Example</b><br>
<b>Example</b><br>
<b>TRObject Syntax</b><br>
 
<pre>
</pre>
<b>Base 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>
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>
{{#seo:|title=Procedures in Clomosy - Clomosy Docs}}
{{#seo:|description=Learn to define and utilize procedures to optimize and streamline your mobile app development.}}

Latest revision as of 11:37, 20 December 2024

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

A procedure is defined using the void keyword. The general format of a procedure definition is as follows.

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


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 start ( { ) 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:

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


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

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));
}

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