| | |
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:
Solved Threads: 0
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.)
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.)
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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.
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.
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
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.
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
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
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
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.
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2008
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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
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
It isn't a bug. Free Pascal is a little more strict about certain things than TP.
Your program should read:
This will compile properly in TP also.
Hope this helps.
Your program should read:
Pascal Syntax (Toggle Plain Text)
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.
Hope this helps.
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:
Sorry about that. Hope this helps.
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)
begin {$ifdef FPC} bill( @jim ) {$else} bill( jim ) {$endif} end.
![]() |
Similar Threads
- RE: Leaked Windows Source Code (IT Professionals' Lounge)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: help please
- Next Thread: Need help whit code
| Thread Tools | Search this Thread |






