Board index » delphi » Problem using functions - Undeclared identifier ?

Problem using functions - Undeclared identifier ?

I seem to have a problem understanding how to use functions.

If somewhere in my implementation section I put a function like:

function IntToBin(Value: LongInt;Size: Integer): String;
   var  i: Integer;
   begin
     Result:='';
     for i:=Size downto 0 do
      begin
        if Value and (1 shl i)<>0 then
          begin
           Result:=Result+'1';
          end else begin
             Result:=Result+'0';
      end;
   end;
end;

And then elsewhere in another procedure like a button click I put:

Y :=IntToBin(X,8);

I get an error:

[Error] Unit1.pas(379): Undeclared identifier: 'IntToBin'

What am I doing wrong or not doing?
This happens alway when I try to use a function, not just this
particular one so it is something basic that I don't understand about
functions.

 

Re:Problem using functions - Undeclared identifier ?


In Pascal, any procedure or function in the implementation section only
knows what comes before it, not what comes after it. Therefore, you must
somehow make "forward" declarations so that they are known. There are two
ways of accomplishing this and each has it's own reasons for using that way:

    1. If the function or procedure does not need to be used outside of the
unit, then you can declare them as _forward_ at the beginning of your
implementation section.

        ex:    implementation

                function MySecondFunction(PassThis: variant): ReturnThis;
forward;

                function MyFirstFunction(igotsome: string): boolean;
                begin
                    if MySecondFunction('SoWhat') <> nil then
                        result := true
                    else
                        result := false;
                end;

                function MySecondFunction(PassThis: variant): ReturnThis:
forward;
                begin
                    if pos(PassThis,'SoYourAreRich,SoWhat!') <> 0 then
                        Result := Whatever
                    else
                        Result := nil;
                end;
                end.

    2. If you declare them in the interface section, other units that USE
this one can see the functions/procedures and call them, just like the
objects and vars declared there. The only difference you make is that you
leave off the "forward" keyword and just declare the function/procedure
header line.

Hope this helps....
Woody

Quote
Dave Alden <d-al...@concentric.net> wrote in message

news:37a506ab.7680918@news.concentric.net...
Quote
> I seem to have a problem understanding how to use functions.

> If somewhere in my implementation section I put a function like:

> function IntToBin(Value: LongInt;Size: Integer): String;
>    var i: Integer;
>    begin
>      Result:='';
>      for i:=Size downto 0 do
>       begin
>         if Value and (1 shl i)<>0 then
>           begin
>            Result:=Result+'1';
>           end else begin
>              Result:=Result+'0';
>       end;
>    end;
> end;

> And then elsewhere in another procedure like a button click I put:

> Y :=IntToBin(X,8);

> I get an error:

> [Error] Unit1.pas(379): Undeclared identifier: 'IntToBin'

> What am I doing wrong or not doing?
> This happens alway when I try to use a function, not just this
> particular one so it is something basic that I don't understand about
> functions.

Re:Problem using functions - Undeclared identifier ?


Your problem is that you have to declare the function in your interface
section separately:

interface
  function IntToBin(Value: LongInt;Size: Integer): String;

implementation

function IntToBin(Value: LongInt;Size: Integer): String;
var
  i: Integer;

begin
  Result:='';

  for i:=Size downto 0 do
    if Value and (1 shl i)<>0 then
      Result:=Result+'1';
    else
      Result:=Result+'0';
end;

procedure MyClass.UseIntToBin;
begin
    Y :=IntToBin(X,8);
end;

If not then the function must be declared above where you use it in the
code.  You could of course make it a method of your class.  You certainly
should not use forward.

Hope that clarifies things.

Alistair

Quote
Dave Alden <d-al...@concentric.net> wrote in message

news:37a506ab.7680918@news.concentric.net...
Quote
> I seem to have a problem understanding how to use functions.

> If somewhere in my implementation section I put a function like:

> function IntToBin(Value: LongInt;Size: Integer): String;
>    var i: Integer;
>    begin
>      Result:='';
>      for i:=Size downto 0 do
>       begin
>         if Value and (1 shl i)<>0 then
>           begin
>            Result:=Result+'1';
>           end else begin
>              Result:=Result+'0';
>       end;
>    end;
> end;

> And then elsewhere in another procedure like a button click I put:

> Y :=IntToBin(X,8);

> I get an error:

> [Error] Unit1.pas(379): Undeclared identifier: 'IntToBin'

> What am I doing wrong or not doing?
> This happens alway when I try to use a function, not just this
> particular one so it is something basic that I don't understand about
> functions.

Other Threads