?
Solved

function questions

Posted on 2003-03-02
4
Medium Priority
?
159 Views
Last Modified: 2010-04-04
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
Comment
Question by:LostInSpace2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 8051991
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
 
LVL 3

Accepted Solution

by:
loop_until earned 200 total points
ID: 8052323
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8052337
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
 

Expert Comment

by:Joshjje
ID: 8052380
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question