Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-09

function overloading question

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bruce Bauman

Can an overloaded function differ only by the return type?

For example:

function ReturnSomething(i :  integer) : integer;
begin
   ReturnSomething := 1;
end;
function ReturnSomething(i :  integer): real;
begin
   ReturnSomething := 2.0;

end; { ReturnReal }

I get bizarre results when I try this. If I define the functions in this order, and then call them as follows:

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

the first function is called in both cases.

If I reverse the order of the function declarations the program fails to compile:

Incompatible types: got "Real" expected "SmallInt"

Should I expect this behavior?

I can file a more complete bug report if needed.

-- Bruce

CONFIDENTIALITY NOTICE: This e-mail is confidential and intended solely for the use of the individual or entity to which it is addressed. If you are not the intended recipient, be advised that you have received this email in error and that any use, dissemination, forwarding, printing or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please delete it from your computer and contact the sender.

Reply by email

Jonas Maebe
to Bruce Bauman
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

Reply by email

David W Noon
to Bruce Bauman: Can an overloaded function differ only by the return type? For example: function…

On Tue, 8 Sep 2009 10:44:36 -0400, Bruce Bauman wrote about [fpc-devel] function overloading question:

Can an overloaded function differ only by the return type?

No.

The specific function has to be selected from the range of overloads by its argument signature. As a result, all the overloads of a given name must have different sequences/types of arguments.

Regards, Dave [RLU #314465] ======================================================================= (David W Noon) =======================================================================

Reply by email