Bug in Free Pascal

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 5
Reputation: gjkeeler is an unknown quantity at this point 
Solved Threads: 0
gjkeeler gjkeeler is offline Offline
Newbie Poster

Bug in Free Pascal

 
0
  #1
Nov 7th, 2008
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 "<function name>"
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.)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Bug in Free Pascal

 
0
  #2
Nov 7th, 2008
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.
Last edited by LizR; Nov 7th, 2008 at 12:47 pm.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: gjkeeler is an unknown quantity at this point 
Solved Threads: 0
gjkeeler gjkeeler is offline Offline
Newbie Poster

Re: Bug in Free Pascal

 
0
  #3
Nov 8th, 2008
Originally Posted by LizR View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Bug in Free Pascal

 
0
  #4
Nov 8th, 2008
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
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: gjkeeler is an unknown quantity at this point 
Solved Threads: 0
gjkeeler gjkeeler is offline Offline
Newbie Poster

Re: Bug in Free Pascal

 
0
  #5
Nov 9th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Bug in Free Pascal

 
0
  #6
Nov 10th, 2008
Would be best to ask them that.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: gjkeeler is an unknown quantity at this point 
Solved Threads: 0
gjkeeler gjkeeler is offline Offline
Newbie Poster

Re: Bug in Free Pascal

 
0
  #7
Nov 14th, 2008
Originally Posted by gjkeeler View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,952
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Bug in Free Pascal

 
0
  #8
Nov 16th, 2008
It isn't a bug. Free Pascal is a little more strict about certain things than TP.
Your program should read:
  1. program fred;
  2.  
  3. type funcparam=function(x:real):real;
  4.  
  5. function jim(x:real):real;
  6. begin
  7. jim:=x
  8. end;
  9.  
  10. procedure bill(func:funcparam);
  11. begin
  12. writeln(func(3):5:2);
  13. end;
  14.  
  15. begin
  16. bill(@jim); { Notice that I am getting the address of the function 'jim' }
  17. end.
This will compile properly in TP also.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: gjkeeler is an unknown quantity at this point 
Solved Threads: 0
gjkeeler gjkeeler is offline Offline
Newbie Poster

Re: Bug in Free Pascal

 
0
  #9
Nov 19th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,952
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Bug in Free Pascal

 
0
  #10
Nov 20th, 2008
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:
  1. begin
  2. {$ifdef FPC}
  3. bill( @jim )
  4. {$else}
  5. bill( jim )
  6. {$endif}
  7. end.
Sorry about that. Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC