0
function F(x:real) : real;
begin
  F := a*power(x,2);
end;

Guys, I need help.I am using Lazarus with form and I want to make a function with variable 'a' there. Variable a is an input just like a:= StrToInt(Edit1.Text); Then how to define a there?

0

1 Answer 1

1

You should make a be an argument to the function.

function F(a: Integer; x: real): real;
begin
  F := a*power(x,2);
end;

Further, using power for integer exponents is expensive and not terribly accurate. Use direct multiplication, in this case you can make use of sqr.

function F(a: Integer; x: real): real;
begin
  F := a*sqr(x);
end;

Now, when you call the function you can use StrToInt(Edit1.Text) to obtain the value of a, which you pass on to the function. Or indeed you can use some other means to obtain a.

All of this is to avoid your arithmetic functions requiring knowledge of your UI.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.