Log in

View Full Version : function



zluo
07-15-2012, 08:20 AM
i have this function to declare something is true.

function Correct : Boolean;
begin
result := true
end;

would this function continue to remain true forever? because this function is set to true near the start of the script and is needed later.

riwu
07-15-2012, 08:32 AM
is this the full function? I dont see the point of this function. What are u going to do with it?

Chris
07-15-2012, 08:34 AM
When you call the function, it will execute everything in there. When you say "Result := True;" at the beginning, it will remain true until you say something like "Result := False;". When you call the function again, the result won't have a value yet.
Im not sure what exactly your question is but I hope this explains it.

zluo
07-15-2012, 09:01 AM
Kinda hard to say what I want to achieve. The function is long and at the end I have result := true. It's more like a procedure but I want the script to know ive executed the function successfully. I want the function to be only executed once but saves the true value so I can say "if correct then..." whenever I want to wIthout re executing the function, get it?

putonajonny
07-15-2012, 09:15 AM
So you have a function like this:
function MyFunction : Boolean;
begin
//Does some stuff
if(DidFunctionCorrectly)then
result := true;
end;

Then you want to call this function:
Repeat
DoThis;
DoThat;
if(MyFunction)then
DoSomethingElse;
Until(false);

However if you want to do multiple things you can save it in a variable:

Var
B : Boolean;
You need a variable to store it in (above)
Repeat
DoThis;
DoThat;
B := MyFunction;
WriteLn('Function Returned: '+ToStr(B));
if(B)then
DoSomethingElse;
Until(false);

zluo
07-15-2012, 10:04 AM
Alright thanks. One more question. After "if (b) then..." can you force set B back to false? Because after I do a loop and reach "if (b) then..." again it would already be set to true and then do whatever is after it even though i don't want it to. also does b := myfunction; execute the function and then stores it in b?

CephaXz
07-15-2012, 10:39 AM
I think its better to put procedure as procedure. I mean if you have a lot of things to do, you might just put it inside a procedure, then use it in the function later.

If you only want the function to return true once, there must be a condition that will happen one time and will not happen any other time. You will that to use that condition to make your function returns as true, so it will only returns true one time.

b := myfunction; will only stores it in B, and will not execute it. So that next time you want to use the function, you don't need to do

if CallingThisEpicAwesomeFunction then
...

//but instead
b := CallingThisEpicAwesoeFunction
if b then
...

There are other usefulness as well like what putonajonny done above.


Tell me if you need an example, I don't think I've explained good enough in words :p
And correct me if I'm wrong :D

P1ng
07-15-2012, 12:51 PM
Just kind of elaborating on putonajonny and CephaXz' explanations, I would set a variable to equal or oppose your function. You cannot change what the function equals without re-executing it, but using a boolean variable you can change what that variable equals with relation to the function without having to re-execute. As shown below (stole putonajonny's master function ;) ) -

program new;

const
a = True;

var
b: Boolean;

function MyFunction : Boolean;
begin
//Does some stuff
if a then
result := true;
end;

begin
b := MyFunction; //Sets the result of b to the same as MyFunction
WriteLn(BoolToStr(b)); //Writes in the debug whether b is true
WriteLn(BoolToStr(MyFunction)); //As you can see they result the same this time
b := not MyFunction; //Makes b the opposite of MyFunction
WriteLn(BoolToStr(b));
WriteLn(BoolToStr(MyFunction)); //They now have opposing results
end.

putonajonny
07-15-2012, 01:08 PM
The result of the function is stored in b, so when you use b the function isn't actually called again.