[fpc-devel] function overloading question

Jonas Maebe jonas.maebe at elis.ugent.be
Tue Sep 8 17:00:05 CEST 2009


On 08 Sep 2009, at 16:44, Bruce Bauman wrote:

> Can an overloaded function differ only by the return type?

No. In fact, the compiler should give a compilation error when trying  
to do that, and it does here:

$ cat tt.pp
function ReturnSomething(i :  integer) : integer;
begin
   ReturnSomething := 1;
end;

function ReturnSomething(i :  integer): real;
begin
   ReturnSomething := 2.0;
end; { ReturnReal }

var
   i: integer;
   r: real;
begin
   i := ReturnSomething(3);
   writeln('i = ', i);
   r := ReturnSomething(4);
   writeln('r = ', r);
end.

$ fpc tt
Free Pascal Compiler version 2.2.4 [2009/04/05] for i386
Copyright (c) 1993-2008 by Florian Klaempfl
Target OS: Darwin for i386
Compiling tt.pp
tt.pp(15,8) Error: Can't determine which overloaded function to call
tt.pp(17,8) Error: Can't determine which overloaded function to call
tt.pp(20) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted


The reason that this is not possible, is because in Pascal the  
compiler does not look at how the result of an expression is used  
while evaluating the expression. In many cases, this is not even  
possible, e.g. what should the compiler here:

writeln(ReturnSomething(3));


Jonas



More information about the fpc-devel mailing list