[fpc-pascal] assigning a local function to a var

David Emerson dle3ab at angelbase.com
Thu Jul 10 22:42:23 CEST 2008


Hi Joao,

You're missing the point -- those functions, func_a and func_b, should 
be embedded within another function. The behavior I would like to see  
concerns local functions, not global functions. In your executable, 
func_a and func_b are global functions (within the unit) not local 
functions (embedded within another function).

As a sidenote, the type declaration is still superfluous -- when dealing 
with global functions, the following will work equally well:

var my_fnc: function(pass_str: ansistring): boolean;

(Although I'm not sure if there might be compiler switches that would 
enable/disable this functionality, possibly requiring the type 
definition. My fpc.cfg may have something enabled that yours doesn't.)

At any rate, global functions work fine, with or without the type 
definition. The reason I posted was to ask about local functions.

~David.


On Thursday 10 July 2008 12:33 pm, Joao Morais wrote:
> David Emerson wrote:
> > Joao: creating a named type isn't a solution, as it only moves the 
> > problem of declaration into the "type" section, where the very same 
> > problem persists. Thanks for responding, though.
> 
> Did you test? I did and it works.
> 
> ==========
procedure parent_procedure;
var parent_var_available_to_local_functions;  // ;)
> function func_a(pass_str: ansistring): boolean;
> begin
>    writeln(pass_str, ' A');
>    func_a := true;
> end;
> 
> function func_b(pass_str: ansistring): boolean;
> begin
>    writeln(pass_str, ' B');
>    func_b := false;
> end;
> 
> type
>    tmy_fnc = function(pass_str: ansistring): boolean;
> 
> var
>    my_fnc: tmy_fnc;
// my_fnc : function(str:ansistring):boolean; // works equally well
> 
> begin
>    my_fnc := @func_a;
>    my_fnc('hello');
> end.
// this is a procedure, so 'end' should be followed by a semicolon,
// not a period.
> ==========
> 
> Joao Morais
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> 




More information about the fpc-pascal mailing list