Forum > General

Can't get function to work

(1/1)

Lazarick:
Hello,
I used Delphi a long time ago and made some programs in those days, I'm absolutely not a pro or a wizkid and my knowledge is very (very) rusty I noticed. But I picked up programming and enjoy Lazarus now. I tried to make a function for a small program I'm making.
I am trying to get it to work for hours now, Googled , Youtubed read tutorials you name it.
Are there some small sample programs someware to look how to do the basic stuff ?
Below is a small test, to try a function, I'm probably making a very stupid mistake.
When I try to run it I get: Wrong number of parameters specified for call to "squared"
(the name of the function)

Thanks for your reaction in advance, Rick

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    function squared:integer;
  private
    { private declarations }
  public

  end;

var
  Form1: TForm1;
  x:integer;
  y:integer;

implementation

{ TForm1 }


procedure TForm1.Button1Click(Sender: TObject);
begin
  x:=10;
  y := squared(x);
  Memo1.Lines.Add(IntToStr(y));
end;

function squared(var x:integer):integer;
begin
    squared:=x*x;
end;


initialization
  {$I unit1.lrs}

end.

paweld:

--- Code: ---unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    //function squared:integer;  -  not declared the parameter
    function squared(var x:integer):integer;
  private
    { private declarations }
  public

  end;

var
  Form1: TForm1;
  x:integer;
  y:integer;

implementation

{ TForm1 }


procedure TForm1.Button1Click(Sender: TObject);
begin
  x:=10;
  y := squared(x);
  Memo1.Lines.Add(IntToStr(y));
end;

function TForm1.squared(var x:integer):integer;
begin
    squared:=x*x;
end;


initialization
  {$I unit1.lrs}

end.
--- End code ---

Best regards
paweld

Wodzu:
Hi Lazarnick,

I would suggest you to declare the function firstly in the interface section of your class, here:


--- Code: ---  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    function squared(var x:integer):integer; // <--- press Shift + Control + C
  private
    { private declarations }
  public
--- End code ---

and when you finish the declaration press the keyboard combination mentioned in my comment. IDE (Lazarus) will automatically create a body of the function for you, so you will won't have to copy it manually and introduce some bugs like in your case.

Regards

Lazarick:
Thanks a lot for your reaction guys !

Rick

Navigation

[0] Message Index

Go to full version