Difference between revisions of "Function"

From Lazarus wiki
Jump to navigationJump to search
(remove extraneous info about functions being procedures; layout; grammar)
Line 1: Line 1:
A '''function''' is a declaration of a [[Routine|routine]] which may be invoked from within the [[Unit|unit]] that declares it, from outside the unit if the function is public, or from within a [[Program|program]], and the routine returns a value as part of its definition.  A routine that does not return a value as part of its definition is a ''[[Procedure|procedure]]'', however, technically a function is also a procedure as well. 
+
== Overview ==
 +
A '''function''' is a declaration of a [[Routine|routine]] which may be invoked  
 +
* from within the [[Unit|unit]] that declares it
 +
* from outside the unit if the function is public,  
 +
* or from within a [[Program|program]]
  
A function which is part of an object is called a [[property]] if it can be assigned a value, and a [[Method|method]] if it cannot be assigned a value.
+
The routine returns a value as part of its definition.  A routine that does not return a value as part of its definition is a ''[[Procedure|procedure]]''.  
<br>
 
<br>
 
  
[[Addition of two integer]]s example:
+
A function which is part of an object is called a [[property]] and can be assigned/return a value (if you can't assign a value, it would be a [[Method|method]])
 +
 
 +
== Examples ==
 +
Addition of two [[integer]]s example:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
 
  function add(c1, c2 : integer) : integer;
 
  function add(c1, c2 : integer) : integer;
 
  begin
 
  begin
  add := c1 + c2;
+
  add := c1 + c2; //or use result := in Object Pascal/Delphi mode
 
  end;
 
  end;
  
 
  var  
 
  var  
   result : integer;
+
   total: integer;
  
 
  begin
 
  begin
   result := add(4, 5);
+
   total := add(4, 5);
   writeln (result); // result is 9
+
   writeln (total); // result is 9
 
  end.
 
  end.
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[category:Pascal]]
 
[[category:Pascal]]

Revision as of 10:45, 17 August 2013

Overview

A function is a declaration of a routine which may be invoked

  • from within the unit that declares it
  • from outside the unit if the function is public,
  • or from within a program

The routine returns a value as part of its definition. A routine that does not return a value as part of its definition is a procedure.

A function which is part of an object is called a property and can be assigned/return a value (if you can't assign a value, it would be a method)

Examples

Addition of two integers example:

 function add(c1, c2 : integer) : integer;
 begin
 add := c1 + c2; //or use result := in Object Pascal/Delphi mode
 end;

 var 
   total: integer;

 begin
   total := add(4, 5);
   writeln (total); // result is 9
 end.