FUNÇÕES - FUNCTIONS

Uma função retorna sempre um valor para a rotina chamadora.

Sintaxe:

function nome (parâmetros):tipo de retorno;
var
    
 lista de variáveis locais;
begin
     
lista de instruções;
end;


Exemplo:

// Função para calcular o Fatorial de um número
function Fatorial (X: Integer) : Integer;
var
  Y, F: Integer;
begin
  F:=1;
  for Y:= 1 to X do
     F:= F * Y;
  Fatorial := F; // Retorno da função
end;
// Procedimento que chama a função Fatorial

procedure TForm1.ButtonClick (Sender:TObject);
var
  N: Integer;
begin
  N:=StrToInt(Edit1.Text);
  Label1.Caption := IntToStr (Fatorial (N)); // Chamada da Função Fatorial
end;