954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bug in Free Pascal

Hi
I'm trying to use Free Pascal for a simple problem - passing a function as a parameter to a procedure.
This works fine in Turbo Pascal, but in Free pascal throws up the error:
Wrong number of parameters in call to ""
In all other respects Free Pascal seems to be a great compiler, but I can't understand how such a basic problem should go undetected.
The following code illustrates what I'm trying to do:

program fred;
type funcparam=function(x:real):real;
function jim(x:real):real;far;
begin
jim:=x
end;
procedure bill(func:funcparam);
begin
writeln(func(3):5:2);
end;
begin
bill(jim);
end.

Can anyone shed any light on this, or tell me the fix. (I know the 'far' is unnecessary in Free Pascal, it's needed for Turbo pascal but it isn't the problem.)

gjkeeler
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Id have thought it would have complained at
jim:=x

as jim the parameter cant be set a value, it returnsone, but I guess theres tha possibility you're making use of the using the name of the function aspect as the return variable.

I guess then the other choice is

bill(jim);

where jim has no values sent to it.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Id have thought it would have complained at jim:=x

as jim the parameter cant be set a value, it returnsone, but I guess theres tha possibility you're making use of the using the name of the function aspect as the return variable.

I guess then the other choice is

bill(jim);

where jim has no values sent to it.

Hi

jim:=x
is the standard pascal method for assigning a value to the function name.

The call
bill(jim);
is indeed the line the compiler objects to (should have said that), but it's correct pascal syntax and compiles and runs under Turbo Pascal with no problem.
Graham Keeler

gjkeeler
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

I wouldnt expect it to, as what is jim called with? you expecting it to call it with 0?

I know the Jim can be, but at the same time think in most modern coding standards its frowned on as it is more confusing to read

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

Hi Again
I have actually tried with
bill(jim());
and with
bill(jim(0));
but it still fails, in the latter case with a convoluted error message.
I didn't intend to get into a discussion about pascal syntax, but
bill(jim); {where jim is a defined function}
means execute procedure bill using jim as the actual function in place of the formal argument (func in my original example).
What I was really looking for was if anyone knows why free pascal does not behave the same as Turbo pascal and the pascal specification.
Regards
Graham

gjkeeler
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Would be best to ask them that.

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 
Hi Again I have actually tried with bill(jim()); and with bill(jim(0)); but it still fails, in the latter case with a convoluted error message. I didn't intend to get into a discussion about pascal syntax, but bill(jim); {where jim is a defined function} means execute procedure bill using jim as the actual function in place of the formal argument (func in my original example). What I was really looking for was if anyone knows why free pascal does not behave the same as Turbo pascal and the pascal specification. Regards Graham

Hi
Just to let you know I've solved this problem now. There is a compiler switch in Free pascal - "try to be like Turbo pascal 7"
What puzzled me was that I thought that Turbo Pascal was following the ISO pascal standard with this method of passing function names.
(I would still be interested in how you are supposed to do it in the Free Pascal dialect - there is information about esoteric stuff available, but not it's variations from standard pascal)
Regards
Graham

gjkeeler
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

It isn't a bug. Free Pascal is a little more strict about certain things than TP.
Your program should read:

program fred;

type funcparam=function(x:real):real;

function jim(x:real):real;
  begin
  jim:=x
  end;

procedure bill(func:funcparam);
  begin
  writeln(func(3):5:2);
  end;

begin
bill(@jim);  { Notice that I am getting the address of the function 'jim' }
end.

This will compile properly in TP also.

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Thanks Duoas
That was the answer to my problem, but I don't think I would ever have dreamed up putting an @ in fornt of the name.
By the way, Turbo pascal DOESN'T accept the @ symbol - not my version 7 anyway).
Regards
Graham

gjkeeler
Newbie Poster
5 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Hmm, you are right. My TP4 and TP5.5 both barf on both @x and addr(x).

This is one of those (relatively few) things that Borland did wrong (stunting the language). Alas. The only to make things work right with both FPC and TP is to use some conditional magic:

begin
{$ifdef FPC}
  bill( @jim )
{$else}
  bill( jim )
{$endif}
end.

Sorry about that. Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You