SRL Archive - OSR Help

Functions...


Crit

2012-06-29 05:17:24

Hello guys. I have read through many, many tutorials but there is one thin I can't figure out. That is parameters of a function.
 function(var x, y : Integer) : Boolean;

I get the fact that the result of the function is Boolean. But inside the paranthesis it has var x and y. This examPle is from kyle undefined's tut and he says the function gives x and y as a result?? Aren't they parameters?
Thanks
-Crit

Ps: sorry for the block of text. on phone atm and I can't see a damn thing.

riwu

2012-06-29 05:24:12

It is impossible for a function to return 2 results the conventional way. i.e. no such thing as function abc: boolean and string;
Hence to get around it, ppl have to load the result into the parameter.
i.e.
function abc(var x: integer): boolean;
begin
x:=blabla;
if blabla then
result:=true;
end;
So basically one of the result is stored as the parameter, and u just have to declare the parameter to use the "x" that the result returned.
A lot of functions, such as FindColor, use this type of parameter-result storing.

Crit

2012-06-29 05:42:49

From: riwu

So basically one of the result is stored as the parameter, and u just have to declare the parameter to use the "x" that the result returned.
A lot of functions, such as FindColor, use this type of parameter-result storing.

Why not store wit as a global variable. And secondly whats the difference between:
function blah(var x: integer):Boolean 
And
 function blah(x: integer):Boolean 
Thanks
-Crit

Caotom

2012-06-29 06:01:15

Okay you use
function blah(x: integer):Boolean
if you want to call the function as say
Blah(1);
the 1 would then be the x. This is used in a script to give the ability to use the same function for a variety of different situations.
As mentioned above, this gives an ease of use in re-use of the function so it would defeat the point to call the variables as global vars.

~Caotom

Crit

2012-06-29 06:05:24

From: Caotom
Okay you use
function blah(x: integer):Boolean
if you want to call the function as say
Blah(1);
the 1 would then be the x. This is used in a script to give the ability to use the same function for a variety of different situations.
As far as I know adding var doesn't make a difference (someone correct me if I am wrong).
As mentioned above, this gives an ease of use in re-use of the function so it would defeat the point to call the variables as global vars.

~Caotom


I know you can call it somewhere else later on. But how does it differ from eg the common autocolor function where it returns Boolean and x/y??
Thanks for your boss answer Caotom lol
-Crit

riwu

2012-06-29 06:13:54

Apologize for confusing u, var is what that makes it a result rather than a parameter. So if u see a var in the parameter it means u have to declare it.

Crit

2012-06-29 06:22:11

From: riwu
Apologize for confusing u, var is what that makes it a result rather than a parameter. So if u see a var in the parameter it means u have to declare it.


Thank you very much mate. My confusion is over!! =D
-Crit

Abu

2012-06-29 06:33:04

#just want to add, there are probably a lot of things which seem a bit 'iffy' to you right now, but the more you start to script the more you understand these matters.

Flight

2012-06-29 06:59:20

From: Crit Boy
Hello guys. I have read through many, many tutorials but there is one thin I can't figure out. That is parameters of a function.
 function(var x, y : Integer) : Boolean;

I get the fact that the result of the function is Boolean. But inside the paranthesis it has var x and y. This examPle is from kyle undefined's tut and he says the function gives x and y as a result?? Aren't they parameters?
Thanks
-Crit

Ps: sorry for the block of text. on phone atm and I can't see a damn thing.


I'm no expert in the field, but I do know that Functions can result in anything, not just limited to booleans.


function IsMoving: Boolean;
function GetMyPos: TPoint;
function CreateBitmap(w, h: integer): Integer;
function Between(s1, s2, str: string): String;

You can see function can result in whatever you want as these are just examples. Also from those examples, they don't have to have parameters at all.

Another key note is if a function has (var something A): (something B); then that means it's, in essence, returning two variables. If any function has a "var" list in the parameters, that means those parameters will be supplied from the function, all you have to do is input an empty variable for the function to populate, in this case it's "something A". The second task is returning a result, this is the "something B". I'll write a quick example:


program New;

{
Two parameters are required here, but because
they're "var" then they'll be populated by
the function. Also, the function returns
a String.
}
Function GetNumbers(var Xvalue, Yvalue: integer): String;
begin
Xvalue := 5;
Yvalue := 10;
result := IntToStr(Xvalue+Yvalue);
end;

Procedure WriteOurValues;
var
X,Y: Integer; //These are our 'empty variables', they will be populated by the function
Total: String; //Same here
begin
Total := GetNumbers(X, Y);
Writeln('Our total is '+Total);
end;

begin
WriteOurValues;
end.

I hope that makes sense. :)

Crit

2012-06-29 09:34:12

From: Flight
I'm no expert in the field, but I do know that Functions can result in anything, not just limited to booleans.


function IsMoving: Boolean;
function GetMyPos: TPoint;
function CreateBitmap(w, h: integer): Integer;
function Between(s1, s2, str: string): String;

You can see function can result in whatever you want as these are just examples. Also from those examples, they don't have to have parameters at all.

Another key note is if a function has (var something A): (something B); then that means it's, in essence, returning two variables. If any function has a "var" list in the parameters, that means those parameters will be supplied from the function, all you have to do is input an empty variable for the function to populate, in this case it's "something A". The second task is returning a result, this is the "something B". I'll write a quick example:


program New;

{
Two parameters are required here, but because
they're "var" then they'll be populated by
the function. Also, the function returns
a String.
}
Function GetNumbers(var Xvalue, Yvalue: integer): String;
begin
Xvalue := 5;
Yvalue := 10;
result := IntToStr(Xvalue+Yvalue);
end;

Procedure WriteOurValues;
var
X,Y: Integer; //These are our 'empty variables', they will be populated by the function
Total: String; //Same here
begin
Total := GetNumbers(X, Y);
Writeln('Our total is '+Total);
end;

begin
WriteOurValues;
end.

I hope that makes sense. :)

Kinda. Are you actually using the result from the function though?? As far as I can see you get the function to output the x and y values which are added in the procedure. But when are you using the result and what does calling the function(x,y) do?
I understand when you have parameters and then call the function/ procedure with the parameters but what do you do with this one??
-Crit

Ps: thanks for your interest in my problem you busy people

riwu

2012-06-29 09:58:24

From: Flight
I'm no expert in the field, but I do know that Functions can result in anything, not just limited to booleans.


function IsMoving: Boolean;
function GetMyPos: TPoint;
function CreateBitmap(w, h: integer): Integer;
function Between(s1, s2, str: string): String;

You can see function can result in whatever you want as these are just examples. Also from those examples, they don't have to have parameters at all.

Another key note is if a function has (var something A): (something B); then that means it's, in essence, returning two variables. If any function has a "var" list in the parameters, that means those parameters will be supplied from the function, all you have to do is input an empty variable for the function to populate, in this case it's "something A". The second task is returning a result, this is the "something B". I'll write a quick example:


program New;

{
Two parameters are required here, but because
they're "var" then they'll be populated by
the function. Also, the function returns
a String.
}
Function GetNumbers(var Xvalue, Yvalue: integer): String;
begin
Xvalue := 5;
Yvalue := 10;
result := IntToStr(Xvalue+Yvalue);
end;

Procedure WriteOurValues;
var
X,Y: Integer; //These are our 'empty variables', they will be populated by the function
Total: String; //Same here
begin
Total := GetNumbers(X, Y);
Writeln('Our total is '+Total);
end;

begin
WriteOurValues;
end.

I hope that makes sense. :)

If u use the Xvalue and Yvalue solely to calculate ur final result (the string), wouldn't that be defeating the purpose of creating the two additional results?
i.e.
Function GetNumbers: String;
begin
result := IntToStr(5+10);
end;
will do the same thing.

From my understanding its meant to be storing a different resultant variable to be used separately, i.e.
a:=GetMMLevels('hp',color);
if a<100 then
begin
writeln('dying! Initializing emergency teleport...');
MouseItem(TAB_SLOT, mouse_left);
end else
if (color='orange') then
begin
writeln('switching to guthan..');
//a procedure to switch to guthan
end;
maybe not a very good example but u get what im trying to say :redface:

Flight

2012-06-29 10:11:51

From: Crit Boy
Kinda. Are you actually using the result from the function though?? As far as I can see you get the function to output the x and y values which are added in the procedure. But when are you using the result and what does calling the function(x,y) do?
I understand when you have parameters and then call the function/ procedure with the parameters but what do you do with this one??
-Crit

Ps: thanks for your interest in my problem you busy people


Yes I'm making use of the result of the function.


Procedure WriteOurValues;
var
X,Y: Integer; //These are our 'empty variables', they will be populated by the function
Total: String; //Same here
begin
Total := GetNumbers(X, Y); //We populate the 'Total' variable with the result from "GetNumbers(X, Y)" The result itself is now 'Total'
Writeln('Our total is '+Total);
end;

From: riwu

If u use the Xvalue and Yvalue solely to calculate ur final result (the string), wouldn't that be defeating the purpose of creating the two additional results?


You're right on the money, Riwu, but I was showing how to make use of Functions that have both a result as well as return variables (is that the correct term? I dunno…).

Crit

2012-06-29 10:41:03

From: Flight
Yes I'm making use of the result of the function.


Procedure WriteOurValues;
var
X,Y: Integer; //These are our 'empty variables', they will be populated by the function
Total: String; //Same here
begin
Total := GetNumbers(X, Y); //We populate the 'Total' variable with the result from "GetNumbers(X, Y)" The result itself is now 'Total'
Writeln('Our total is '+Total);
end;



You're right on the money, Riwu, but I was showing how to make use of Functions that have both a result as well as return variables (is that the correct term? I dunno…).

I understand that you are making use of the x and y variables from the function but what happened to the result string? Is going to be displayed in the debug box. Anyway if I am just I confusing you too much with my questions I will just take ur examples and try them at home. Won't be for another week though.
Thanks for your help anyway.
-Crit

riwu

2012-06-29 10:51:43

From: Crit Boy
I understand that you are making use of the x and y variables from the function but what happened to the result string? Is going to be displayed in the debug box. Anyway if I am just I confusing you too much with my questions I will just take ur examples and try them at home. Won't be for another week though.
Thanks for your help anyway.
-Crit

The result string get stored in another string variable "Total", u can do anything u want with result string, but in this case it will appear at the debug box since writeln is used.

Crit

2012-06-29 11:08:13

From: riwu
The result string get stored in another string variable "Total", u can do anything u want with result string, but in this case it will appear at the debug box since writeln is used.


Oh ok so because we declared 'Total' as a string it only accepts the string part of the function and not the x and y integers??

Flight

2012-06-29 11:16:32

From: Crit Boy
Oh ok so because we declared 'Total' as a string it only accepts the string part of the function and not the x and y integers??


Correct, and because the "result type" of the function is a String rather than a Boolean we can store the result as a variable we declare, in this case 'Total'.

Crit

2012-06-29 11:26:38

From: Flight
Correct, and because the "result type" of the function is a String rather than a Boolean we can store the result as a variable we declare, in this case 'Total'.


Yay I finally understand it!! Just one more question.
If we had this:
program New;

* Function GetNumbers(var number: integer): String;
* begin
* * Number := 15
* * result := 'String result';
* end;

* Procedure WriteOurValues;
* var
* * Number, total: Integer;
* begin
* * Total := GetNumbers(Number);
* * Writeln('Our total is '+IntToStr(Total));
* end;

begin
* WriteOurValues;
end.
Would It use number as a result or am I dumb…? I know th coding might be a bit wrong but are basics right?
-Crit

Ps: don't know why there is asterisks in SIMBA code. iPod is retarded..

riwu

2012-06-29 20:46:27

From: Crit Boy
Yay I finally understand it!! Just one more question.
If we had this:
program New;

* Function GetNumbers(var number: integer): String;
* begin
* * Number := 15
* * result := 'String result';
* end;

* Procedure WriteOurValues;
* var
* * Number, total: Integer;
* begin
* * Total := GetNumbers(Number);
* * Writeln('Our total is '+IntToStr(Total));
* end;

begin
* WriteOurValues;
end.
Would It use number as a result or am I dumb…? I know th coding might be a bit wrong but are basics right?
-Crit

Ps: don't know why there is asterisks in SIMBA code. iPod is retarded..

Nope. When u call a function by its function name (with its respective parameters/parameter-results), it will only return the result type of the function:
function FunctionName(parameters: integer):String;

So when u call this function, it will always return the result type that comes after ":", which is a string. To make use of the results u stored in the parameter, u will just use the declared variable.
program New;

Function GetNumbers(var number: integer): String;
begin
Number := 15;
result := 'String result';
end;

Procedure WriteOurValues;
var
Number : Integer;
StringStorer: String;
begin
StringStorer:=GetNumbers(Number); //when u call this function, it will always return the string since that is its output
writeln(StringStorer); //this will hence returns 'String result', which is what u defined as the output result of the function

//If u want to make use of the results in the parameter, just name it:
Writeln('Our number is '+IntToStr(Number));
//note that u'll have to call the function first before u can use its parameter-result.
end;

begin
WriteOurValues;
end.

A simpler example would be FindColor(x,y,12345,1,1,1,1):boolean;
So u would usually use the function by:

procedure abc;
var
x,y: integer; //declare the parameter-result
begin
if FindColor(x,y,12345,1,1,1,1) then //its making use of its output result (the boolean) for the conditional statement
Mouse(x,y,5,5,true); //So after u called the function, u can just use the parameter result (x,y) in another function as long as its called within the same procedure as u called FindColor (unless u declare x,y globally which is not recommended)

writeln(toStr(x)+','+toStr(y)); //see how i can just freely use the parameter result anytime (within the same procedure) again?
end;
Just an example though, u shd always use tolerance in ur rs scripts.

Brandon

2012-06-29 21:01:16

From: riwu
It is impossible for a function to return 2 results the conventional way. i.e. no such thing as function abc: boolean and string;
Hence to get around it, ppl have to load the result into the parameter.


I concur..


type Results = record
ReturnValue: Boolean;
X: Integer;
Y: String;
end;

Function ReturnTwoValues: Results;
begin
Result.ReturnValue:= True;
Result.X:= 5;
Result.Y:= 'SomeString';
end;

Function ReturnMostValues: TVariantArray;
begin
SetLength(Result, 3);
Result[0]:= True;
Result[1]:= 5;
Result[2]:= 'SomeString';
end;

begin
WriteLn(ReturnTwoValues);
WriteLn(ReturnMostValues);
end.

[BANANA]
Compiled successfully in 16 ms.
(1, 5, 'SomeString')
[True, 5, 'SomeString']
Successfully executed.
[/BANANA]

Crit

2012-06-30 04:21:54

From: riwu

A simpler example would be FindColor(x,y,12345,1,1,1,1):boolean;
So u would usually use the function by:

procedure abc;
var
x,y: integer; //declare the parameter-result
begin
if FindColor(x,y,12345,1,1,1,1) then //its making use of its output result (the boolean) for the conditional statement
Mouse(x,y,5,5,true); //So after u called the function, u can just use the parameter result (x,y) in another function as long as its called within the same procedure as u called FindColor (unless u declare x,y globally which is not recommended)

writeln(toStr(x)+','+toStr(y)); //see how i can just freely use the parameter result anytime (within the same procedure) again?
end;
Just an example though, u shd always use tolerance in ur rs scripts.

I know about tolerance and tpa and stuff. Also in findcolor wouldn't you have to add "var" inside the paranthesis? And how do you declare a parameter and a parameter-result at the same time?
And Brandon I am not too comfortable with types and records.
Thanks for all your answers. This has cleared up a lot.
-Crit

riwu

2012-06-30 04:28:20

From: Crit Boy
I know about tolerance and tpa and stuff. Also in findcolor wouldn't you have to add "var" inside the paranthesis? And how do you declare a parameter and a parameter-result at the same time?
And Brandon I am not too comfortable with types and records.
Thanks for all your answers. This has cleared up a lot.
-Crit

u only add "var" when u are creating the function, im calling an existing function so the parameter is just x and y.
when i use the term "parameter-result" i just mean the "var parameter", not really too sure what its formally called. U dont have to declare output results. (unless u want to make a new variable to store the output result, like 'Total' in the previous examples)

Crit

2012-06-30 06:32:26

From: riwu
u only add "var" when u are creating the function, im calling an existing function so the parameter is just x and y.
when i use the term "parameter-result" i just mean the "var parameter", not really too sure what its formally called. U dont have to declare output results. (unless u want to make a new variable to store the output result, like 'Total' in the previous examples)


Sorry of course… Wow I'm a derp. For the second question I mean what of you need two results but also want to call a function's or procedure's parameters in the main loop. Does it work? The answer is not too important as you have fully answered my original question. Tyvm
-Crit

putonajonny

2012-06-30 07:01:55

From: Crit Boy
Sorry of course… Wow I'm a derp. For the second question I mean what of you need two results but also want to call a function's or procedure's parameters in the main loop. Does it work? The answer is not too important as you have fully answered my original question. Tyvm
-Crit


Yes, I'll show you what adding Var in the parameter does:
Say we have a function like this:
program new;
Var
x, y, z : integer;

Function AddTheseNumbers(Num1, Num2 : integer) : integer;
begin

Result := Num1 + Num2;

end;

begin

x := 2;
y := 3;
z := AddTheseNumbers(x, y);
WriteLn('x = '+IntToStr(x)+' y = '+IntToStr(y) + ' z = '+IntToStr(z));

end.
As you would expect you get this:
x = 2 y = 3 z = 5

But what about if we want to set the numbers to 0 once we have added them:
program new;
Var
x, y, z : integer;

Function AddTheseNumbers(Num1, Num2 : integer) : integer;
begin

Result := Num1 + Num2;
Num1 := 0;
Num2 := 0;

end;

begin

x := 2;
y := 3;
z := AddTheseNumbers(x, y);
WriteLn('x = '+IntToStr(x)+' y = '+IntToStr(y) + ' z = '+IntToStr(z));

end.
This gives out the exact same thing:
x = 2 y = 3 z = 5
We have to tell the function that it is allowed to change the values stores in Num1 and Num2, we do this by adding Var before we declare them, like this:
Function AddTheseNumbers(Var Num1, Num2 : integer) : integer;
Then when we run it:
x = 0 y = 0 z = 5
Because they were Variables (not Constants) the values in them could be changed in the function.

A good example for when this is used is in FindColorSpiralTolerance, you could look at that

Crit

2012-06-30 07:40:32

From: putonajonny

We have to tell the function that it is allowed to change the values stores in Num1 and Num2, we do this by adding Var before we declare them, like this:
Function AddTheseNumbers(Var Num1, Num2 : integer) : integer;
Then when we run it:
x = 0 y = 0 z = 5
Because they were Variables (not Constants) the values in them could be changed in the function.

A good example for when this is used is in FindColorSpiralTolerance, you could look at that

Ah ok I get this now. Why if you want the function to be able to change some I the values inside the parameter but not all of them? Would you just not change them inside the function?
Thanks anyway for your detailed answer!
-Crit

riwu

2012-06-30 07:47:55

From: Crit Boy
Ah ok I get this now. Why if you want the function to be able to change some I the values inside the parameter but not all of them? Would you just not change them inside the function?
Thanks anyway for your detailed answer!
-Crit

Any value that u didnt put a var in front will not be changed. i.e.
function FindColor(var x,y: integer; color, blabla: integer):boolean;
Only the value x,y will be dynamic (depending on what u assign them to in the function), and the rest that doesn't have "var" will just be parameters (which u decide what are they when u call the function)

Crit

2012-06-30 09:51:31

From: riwu
Any value that u didnt put a var in front will not be changed. i.e.
function FindColor(var x,y: integer; color, blabla: integer):boolean;
Only the value x,y will be dynamic (depending on what u assign them to in the function), and the rest that doesn't have "var" will just be parameters (which u decide what are they when u call the function)

Ok that's exactly the answer I was looking for riwu. I can't thank you enough. You should really get the SRL-Helper title for being so helpful.=D
Thanks to everybody else who contributed to the answer.
-Crit

Ps: admins can close this now of they want