SRL Archive - OSR Help

Confusion


Wire

2014-06-14 14:03:16

I'm currently following Cohen beginners script tutorial and do not fully understand why this method would be used over the one i provided. I understand he was attempting to provide an example of function but wouldn't it be far easier to use procedure than function in this scenario? I believe the reason i do not understand his example fully is because there is a local variable, so it would only effect this section and not furthering anything else. Please explain why this would be more ideal over procedure as i'm most likely wrong. Thanks Wire_

Sidenote- Why does he include string; in the name? I understand function will return a answer while procedure will not. So the main purpose of function would be to receive an answer that can further the script like failsafes or checking for certain objects?


function WeAreBored: string;
var
s: String;
begin
Result := 'What are we going to do now?';
s := 'We are very bored chopping all these logs!';
Wait(500);
WriteLn(s);
end;


VERSUS


procedure WeAreBoredTest;
begin;
WriteLn('What are we going to do now?');
WriteLn('We are very bored chopping all these logs!');
Wait(500);
end;

Ian

2014-06-14 14:18:42

In this scenario it would be easier to use a procedure, yes.

And the purpose of functions is to return results. So you could have something like

function isSomePixelBlack: Boolean; //checks whether the pixel at (100, 100) is black
begin
result:= (getColor(100, 100) = 0);
end;
However this example is not very useful because it can only check one pixel. A more useful function would be


function isPixelBlack(Pixel: TPoint): Boolean; //checks whether a given is black based on the parameter "Pixel"
begin
result:= (getColor(Pixel) = 0);
end;
this would be used like so

if isPixelBlack(point(100, 100)) then
writeLn('given pixel is black');
These examples are so basic that they're not very useful in scripts, but they are to give an idea of how functions work.

Also I just did this in the browser so hopefully I didn't make any mistakes.

masterBB

2014-06-14 14:41:32

Imagine if FindColorX wasn't a function but a procedure, how would you know it found something ;)

Wire

2014-06-14 15:20:57

Replay To BMW
Thanks for the responses, but another questions has arisen.I think its better if i try to expand the example you provided with the areas i do understand.

function isSomePixelBlack: Boolean; //function is the other version of command, and isSomePixelBlack a specific command, and Boolean is true/false.
begin //starts
result:= (getColor(100, 100) = 0); //what is the purpose of result? Sense if Boolean is true or false wouldn't it make this statement possible irregardless? From my understanding it basically means, If some pixel is black then the Boolean returns true answering the function?
end; //ends

-MasterBB
Yes i thought that is why function was used for and is used in conjunction with Boolean usually?

masterBB

2014-06-14 17:17:27

From: Wire
Replay To BMW
Thanks for the responses, but another questions has arisen.I think its better if i try to expand the example you provided with the areas i do understand.

function isSomePixelBlack: Boolean; //function is the other version of command, and isSomePixelBlack a specific command, and Boolean is true/false.
begin //starts
result:= (getColor(100, 100) = 0); //what is the purpose of result? Sense if Boolean is true or false wouldn't it make this statement possible irregardless? From my understanding it basically means, If some pixel is black then the Boolean returns true answering the function?
end; //ends

-MasterBB
Yes i thought that is why function was used for and is used in conjunction with Boolean usually?


function isSomePixelBlack: Boolean;
begin
result := (getColor(100, 100) = 0);
end;

use simba tags please.

Result is just variable. When the function reaches the end the variable is returned.

All these functions might be a tad to simple though. Let me show you something else(wrote in browser):
function QuadraticFormule(A, B, C: Extended): Array of Double;
var
D: Extended;
begin
D := sqr(B) - (4 * A * C);
if(D < 0) then
begin
SetLength(result, 1);
result[0] := 0;
writeln('D is negative');
end else
begin
SetLength(result, 2);
result[0] := (D - B) / (2 * A);
result[1] := (D + B) / (2 * A);
end;
end;

Ian

2014-06-14 17:21:00

From: Wire
Replay To BMW
Thanks for the responses, but another questions has arisen.I think its better if i try to expand the example you provided with the areas i do understand.

function isSomePixelBlack: Boolean; //function is the other version of command, and isSomePixelBlack a specific command, and Boolean is true/false.
begin //starts
result:= (getColor(100, 100) = 0); //what is the purpose of result? Sense if Boolean is true or false wouldn't it make this statement possible irregardless? From my understanding it basically means, If some pixel is black then the Boolean returns true answering the function?
end; //ends

-MasterBB
Yes i thought that is why function was used for and is used in conjunction with Boolean usually?

 
result:= (getColor(100, 100) = 0);

Would be true whenever getcolor(100, 100) returned 0. When it returned as something other than 0 the result would be false.

And functions can return more than booleans. For example getcolor is a function that returns an integer.

The Mayor

2014-06-14 17:49:07

I think newbs find it easier to understand a function when you use a conditional to assign the result.

The following boolean function is going to generate a random number out of 20. If it equals 10 then the function results true.

function isRandomNumber10(): boolean;
begin

end;

The integer variable 'x' is assigned a value out of 20 (between 0..19). If x equals 10 then the function results true.

function isRandomNumber10(): boolean;
var
x: integer;
begin
x := random(20);

if x = 10 then
result := true;
end;

This is the same as:


function isRandomNumber1(): boolean;
var
x: integer;
begin
x := random(20);
result := x = 10; // result true if x = 10
end;

Functions don't have to be booleans. Here is an example of a function returning an integer as the result.


function timesByTen(number: integer): integer;
begin
result := number * 10;
writeLn(toStr(number) + ' multiplied by 10 equals: ' + toStr(result));
end;

This function takes a number, multiplies it by 10, and returns the new number as the result. So the output of this little program:



program new;

function timesByTen(number: integer): integer;
begin
result := number * 10;
writeLn(toStr(number) + ' multiplied by 10 equals: ' + toStr(result));
end;

begin
timesByTen(10);
end.


would be:


Compiled successfully in 250 ms.
10 multiplied by 10 equals: 100
Successfully executed.

Wire

2014-06-14 18:19:22

Okay, i think i understand but we shall see when i start posting question from my script failures :D. Thanks everyone for answering and explaining in a little more depth about the topic.