我写了一个函数(以前很少用它)function Isprime(i:integer):boolean
放在private 下面,用这个函数在判断一个数是不是素数,我用了对result
和isprime赋值的方法,但都会产生一个错误,我是个初学者,大家能不能告
诉我函数是怎么使用的吗?

解决方案 »

  1.   

    返回值类型不对;
    function Isprime(i:integer):boolean
    定义了返回值是逻辑型,那么返回当然就要用逻辑的。
      

  2.   

    是呀!
    我返回语句是:
    result:=false;  or  result:=true;
    Isprime:=false;  of isprime:=true;
      

  3.   

    是不是你在写函数体的时候,没有写窗体的名字啊?
    声明函数:
    private
        function Isprime(i:integer):boolean;
    实现部分:
    function TForm1.Isprime(i:integer):boolean;
    beginend;
    注意:TForm1
      

  4.   

    [Error] Unit1.pas(17): Unsatisfied forward or external declaration: 'TForm1.Isprime'
      

  5.   

    program Project1;uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    /////////////////
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function Isprime(i:integer):boolean;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
    i,j :Integer;
    begin
    i:=StrToInt(Edit1.text);
    if i>5 then
      for j:=2 to i do
        if isprime(j) and isprime(i div j) then
           listbox1.AddItem(inttostr(j)+'*'+inttostr(i div j),Listbox1);
    end;function IsPrime(i:integer):boolean;
    var
     Byes:boolean;
     j:integer;
    begin
     Byes:=true;   //标志
     for j:=2 to i do
       if i mod j=0 then
       Byes:=false;
     Isprime:=Byes;
    end;
    end.
      

  6.   

    //////////
    错误信息[Error] Unit1.pas(17): Unsatisfied forward or external declaration: 'TForm1.Isprime'
    [Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
      

  7.   


    function IsPrime(i:integer):boolean;
    这部分应该改为:
    function TForm1.IsPrime(i:integer):boolean;
      

  8.   

    错误信息不是都已经给你指出来了吗?
    Unsatisfied forward or external declaration: 'TForm1.Isprime'
      

  9.   

    program Project1;uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    /////////////////
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        Edit1: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        function Isprime(i:integer):boolean;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
    i,j :Integer;
    begin
    i:=StrToInt(Edit1.text);
    if i>5 then
      for j:=2 to i do
        if isprime(j) and isprime(i div j) then
           listbox1.AddItem(inttostr(j)+'*'+inttostr(i div j),Listbox1);
    end;function TForm1.IsPrime(i:integer):boolean;  //或者把IsPrime从TForm1类里拿出来
    var
     Byes:boolean;
     j:integer;
    begin
     Byes:=true;   //标志
     for j:=2 to i do
       if i mod j=0 then
       Byes:=false;
     Isprime:=Byes;
    end;
    end.