Results 1 to 8 of 8

Thread: Confusion

  1. #1
    Join Date
    Jun 2014
    Location
    EastCoast
    Posts
    76
    Mentioned
    0 Post(s)
    Quoted
    31 Post(s)

    Lightbulb Confusion

    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;

  2. #2
    Join Date
    Jun 2012
    Posts
    4,867
    Mentioned
    74 Post(s)
    Quoted
    1663 Post(s)

    Default

    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
    Simba Code:
    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

    Simba Code:
    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
    Simba Code:
    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.

  3. #3
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Imagine if FindColorX wasn't a function but a procedure, how would you know it found something
    Working on: Tithe Farmer

  4. #4
    Join Date
    Jun 2014
    Location
    EastCoast
    Posts
    76
    Mentioned
    0 Post(s)
    Quoted
    31 Post(s)

    Default

    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?
    Last edited by Wire; 06-14-2014 at 07:23 PM.

  5. #5
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by Wire View Post
    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?
    Simba Code:
    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):
    Simba Code:
    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;
    Working on: Tithe Farmer

  6. #6
    Join Date
    Jun 2012
    Posts
    4,867
    Mentioned
    74 Post(s)
    Quoted
    1663 Post(s)

    Default

    Quote Originally Posted by Wire View Post
    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?
    Simba Code:
    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.

  7. #7
    Join Date
    Jun 2007
    Location
    The land of the long white cloud.
    Posts
    3,702
    Mentioned
    261 Post(s)
    Quoted
    2006 Post(s)

    Default

    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.
    Simba Code:
    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.
    Simba Code:
    function isRandomNumber10(): boolean;
    var
      x: integer;
    begin
      x := random(20);

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

    This is the same as:

    Simba Code:
    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.

    Simba Code:
    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:


    Simba Code:
    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:

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

  8. #8
    Join Date
    Jun 2014
    Location
    EastCoast
    Posts
    76
    Mentioned
    0 Post(s)
    Quoted
    31 Post(s)

    Default

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •