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.)

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.

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

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

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

Would be best to ask them that.

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

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.