Function

From Lazarus wiki
Revision as of 00:11, 13 May 2018 by Kai Burghardt (talk | contribs) (expr)
Jump to navigationJump to search

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru)

A function is a routine that, in contrast to procedures, returns a value. A call of a function is virtually substituted by its return value. If the {$extendedSyntax} compiler switch state is off, function calls can not appear as non-productive statements, but have to be or be part of an expression.


The word function is a reserved word.

return value

In addition to a normal procedure, a function's formal signature contains a return type: The formal parameter list has to be succeeded by a colon and return type. For instance the following function returns a boolean.

function myFunction(const firstParameter: real): boolean;

When implementing functions there are several ways to define the function's return value.

program functionDemo(input, output, stderr);

{$mode objfpc}

// traditional syntax:
// the result is stored in the variable
// its name is the same as the function's
function myLine(const x: real): real;
begin
	myLine := 0.5 * x + 2;
end;

If {$modeswitch result on}, which is set by {$mode objFPC} and {$mode Delphi}, inside the implementation block the special identifier result is available, too:

// using special `result` identifier
function myParabola(const x: real): real;
begin
	result := x * x - 1;
end;

Additionally, in {$mode objFPC} the routine exit will set the return value, too, and leave the stack frame. In the previous two examples further statements could have appeared, and they would have been executed, whilst after an exit the routine is done. This is the behavior a return statement in C or other programming languages has.

// using exit routine
function even(const x: longint): boolean;
begin
	exit(not odd(x));
end;

In assembly language other rules apply. If the return type is an integral value, the accumulator register is used, provided it fits in there:

// in assembly language:
// return type fits into a single register => use accumulator register
function zero(const x: int64): boolean;
{$ifdef CPUx86_64}
assembler; register;
{$asmMode intel}
asm
	// xor modifies flags => put it in front of test
	xor rax, rax     // rax := 0 [false]
	
	// examining the assembler output
	// we can verify x is stored in register rdi [i.e. not rax]
	test x, x        // x = 0 ?
	jnz @zero_done   // if x <> 0 then goto done
	
	inc rax          // rax := 1 [true]
@zero_done:
	// When you examine the assembler output
	// you will notice the compiler automatically inserts code
	// that moves the contents of rax to the right spot on the stack.
end;
{$else}
begin
	// NOTE: with optimization switches enabled
	//       the compiler produces with the following Pascal statement
	//       even shorter (and maybe faster) code
	//       than the assembler implementation above
	result := x = 0;
end;
{$endif}

Otherwise, depending on which {$asmMode} is active, the @result (Intel) or __result (AT&T) macro can be used.

begin
end.

Originally Pascal expected exact one assignment to the result variable (whichever is used). FPC however does allow multiple assignments.