943,840 Members | Top Members by Rank

Ad:
Nov 7th, 2008
0

Bug in Free Pascal

Expand Post »
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.)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gjkeeler is offline Offline
5 posts
since Nov 2008
Nov 7th, 2008
0

Re: Bug in Free Pascal

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.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Nov 8th, 2008
0

Re: Bug in Free Pascal

Click to Expand / Collapse  Quote originally posted by LizR ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gjkeeler is offline Offline
5 posts
since Nov 2008
Nov 8th, 2008
0

Re: Bug in Free Pascal

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
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Nov 9th, 2008
0

Re: Bug in Free Pascal

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gjkeeler is offline Offline
5 posts
since Nov 2008
Nov 10th, 2008
0

Re: Bug in Free Pascal

Would be best to ask them that.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Nov 14th, 2008
0

Re: Bug in Free Pascal

Click to Expand / Collapse  Quote originally posted by gjkeeler ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gjkeeler is offline Offline
5 posts
since Nov 2008
Nov 16th, 2008
0

Re: Bug in Free Pascal

It isn't a bug. Free Pascal is a little more strict about certain things than TP.
Your program should read:
Pascal Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 19th, 2008
0

Re: Bug in Free Pascal

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gjkeeler is offline Offline
5 posts
since Nov 2008
Nov 20th, 2008
0

Re: Bug in Free Pascal

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:
Pascal Syntax (Toggle Plain Text)
  1. begin
  2. {$ifdef FPC}
  3. bill( @jim )
  4. {$else}
  5. bill( jim )
  6. {$endif}
  7. end.
Sorry about that. Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: help please
Next Thread in Pascal and Delphi Forum Timeline: Need help whit code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC