Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

function questions

Hi Experts

I`m attempting to learn functions but I seem to be lost. I wanted a function that takes the integer entry in a editbox and multiplies that value by 2.
One of the error messages states[Error] Unit1MainCab32.pas(77): Undeclared identifier: 'edCabQty'

Thanks for the help

function PcOneCount (Pc: Integer) : Integer;
begin
 Pc : = StrToInt(edCabQty.Text);
  PcOneCount := Pc * 2;
end;
0
LostInSpace2
Asked:
LostInSpace2
1 Solution
 
Lukasz LachCommented:
add
function PcOneCount (Pc: Integer) : Integer;
to public in your form cladd and should be in implementation:

function TForm1.PcOneCount (Pc: Integer) : Integer;
begin
  Pc : = StrToInt(edCabQty.Text);
  Result := Pc * 2;
end;


btw what is PC variable for in the funcion? ;-)
that will work better:

function TForm1.PcOneCount: Integer;
begin
  Result : = StrToInt(edCabQty.Text)*2;
end;
0
 
loop_untilCommented:
You have four choices, depending on what you want to do. Anyway, it'll show you what functions can do:

====================================================

function TForm1.PcOneCount: Integer;
begin
 Result := StrToInt(edCabQty.Text) * 2;
end;

How to call this:

begin
  ShowMessage(IntToStr(PcOneCount));
end;

WHAT IT DOES: It will return directly twice your edCabQty.Text value as an Integer when called. This is the simplest method.

====================================================
====================================================

function TForm1.PcOneCount(Pc: Integer): Integer;
begin
  Result := PC * 2;
end;

And call your function like this:

begin
  PcOneCount(StrToInt(edCabQty.Text));
end;

WHAT IT DOES: It will return twice whatever you input in your function. In this case, the input value of your function is the edCabQty.Text cast into an Integer.

====================================================
====================================================

function TForm1.PcOneCount(var Pc: Integer): Integer;
begin
  Pc := StrToInt(edCabQty.Text);
  Result := PC * 2;
end;

Call this function like this:

var
  Temp: Integer;
begin
  ShowMessage(IntToStr(PcOneCount(Temp)));
  ShowMessage(IntToStr(Temp)); // Will be edCabQty.Text now
end;

WHAT IT DOES: This will return a value (which is twice the edCabQty) but also, the input value is changeable by the function (look at the var keyword in PcOneCount(var Pc: Integer)). This means, the variable we assign to the input value (Temp in this case) will be modified and when function returns, it now contains edCabQty value. Look carefully you'll understand. I think (if I didn't explain to badly). ;-)

====================================================
====================================================

function TForm1.PcOneCount: Integer;
var
  Pc: Integer;
begin
  Pc := StrToInt(edCabQty.Text);
  Result := PC * 2;
end;

WHAT IT DOES: This is simply the same solution than the first but you also use a local variable (this means it will not exists anywhere else when function is done). It assign to the local variable the edCabQty and then double it to return this. This might be good if you need to use a couple of times the PC value and don't want to retype each time the StrToInt(edCabQty.Text).


Hope it helps!
Have a nice day! :-)
0
 
kretzschmarCommented:
you must also qualify the object where
edCabQty.Text resides, if your function is not
a method of this object

guessing your object is form1 use

form1.edCabQty.Text

instead of

edCabQty.Text

meikl ;-)
0
 
JoshjjeCommented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
  Function Multiply(Number: Integer): Integer;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function Multiply(Number: Integer): Integer;
begin
Form1.Edit1.Text := IntToStr(StrToInt(Form1.Edit1.Text)*2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Multiply(2);
end;

end.


All you have to do to call the function is Multiply(someinteger), so you would type Multiply(2); to multiply the contents of Edit1.text by 2.  You must put the function in the Uses part of the program as shown above.

To use this just create a new application, add an Edit box and a button, and your good to go.

-Josh-



0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now