Difference between revisions of "Function"
From Free Pascal wiki
Jump to navigationJump to searchm |
|||
Line 4: | Line 4: | ||
<br> | <br> | ||
<br> | <br> | ||
+ | |||
+ | [[Addition of two integer]]s example: | ||
+ | |||
+ | <syntaxhighlight> | ||
+ | function add(c1, c2 : integer) : integer; | ||
+ | begin | ||
+ | add := c1 + c2; | ||
+ | end; | ||
+ | |||
+ | var | ||
+ | result : integer; | ||
+ | |||
+ | begin | ||
+ | result := add(4, 5); | ||
+ | writeln (result); // result is 9 | ||
+ | end. | ||
+ | |||
+ | </syntaxhighlight> | ||
[[category:Pascal]] | [[category:Pascal]] |
Revision as of 16:17, 16 August 2013
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, 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, however, technically a function is also a procedure as well.
A function which is part of an object is called a property if it can be assigned a value, and a method if it cannot be assigned a value.
Addition of two integers example:
function add(c1, c2 : integer) : integer;
begin
add := c1 + c2;
end;
var
result : integer;
begin
result := add(4, 5);
writeln (result); // result is 9
end.