Results 1 to 17 of 17

Thread: Procedure and Functions{How To Use/Differences Between Them}

  1. #1
    Join Date
    Jul 2007
    Location
    America
    Posts
    421
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Procedure and Functions{How To Use/Differences Between Them}

    Procedures and Functions
    by Kik

    Welcome to my tutorial (in progress) on Procedures and Functions!

    Yes, there are like 5 other tuts on this

    *Under construction*

    *assumes you know how the very basics of SCAR : EX: How to make it say 'hello' in the Debug Box*

    Chapters...
    1.0 ) What is a Procedure and a Function?
    2.0 ) Procedures - How do I make one and how do they work?
    3.0 ) Functions - How do I make one and how do they work?
    4.0 ) Declaring Procedures and Functions with Variables (What?)




    1.0 ) What is a Procedure and a Function?


    What's a procedure? -
    You can think of procedures as pre-defined sets of directions or instructions for SCAR to carry out. They make scripting much easier so that you don't have to type the same code over and over again if you want to use it multiple times.

    What's a function? -
    A function is extremely similar to a procedure, except a function returns something. Whoah! What do you mean by 'returns something'? Don't worry, I'll get to that in a little. First lets start off with procedures.


    2.0 ) Procedures - How do I make one and how do they work?


    It is very easy to add your own procedure to any script. Here's how.

    First, lets start off with the basic SCAR setup.
    SCAR Code:
    program new;
    begin
    end.

    Now, lets say we want to make a procedure that writes out 'Kik is teh awesome!' using WriteLn.
    SCAR Code:
    Writeln('Kik is teh awesome!');

    So, lets get to the procedure. The basic setup for a procedure goes as follows.
    SCAR Code:
    procedure WriteStuff;  //You can replace 'WriteStuff' with whatever name your procedure has.
    begin
    //The stuff you want the procedure to do.
    end;

    the 'begin' tells the procedure where the code will start. the 'end;', as you have probably guessed, tells the procedure where the code is ending.

    So, now we simply add the code above with the code above it to make...
    SCAR Code:
    procedure WriteStuff;
    begin
      Writeln('Kik is teh awesome!');
    end;

    Tada! Your first procedure (or your tenth, if you've already been experimenting). Now, lets add that into the script. To do so, you simply place the procedure code within the script as follows...
    SCAR Code:
    program new;

    procedure WriteStuff;
    begin
      Writeln('Kik is teh awesome!');
    end;

    begin
    end.

    But how do we let SCAR know when to perform the procedure WriteStuff? Well, that's easy! All you need to do is write the name of the procedure within the regular script to tell SCAR what to do, like this.
    SCAR Code:
    program new;

    procedure WriteStuff;
    begin
      Writeln('Kik is teh awesome!');
    end;

    begin
      WriteStuff; //this tells SCAR to perform WriteStuff.
    end.

    Now run that script. See what happens? Wasn't that fun! Now you can see why procedures are a neccesity to SCAR scripters.


    3.0 ) Functions - How do I make one and how do they work?


    Functions are slightly more complicated, but have no fear! Kik is here!

    As I said before, functions act almost similarly to procedures, however, as I also said before, they return something. This simply means that the function is equal to something, say an integer or a string (examples below). So, lets say your function returns an integer. You could therefore make, say, variable X:= MyFunction. But how do we let the function know what to return? I'll cover that.

    First, lets start off with the basic syntax for a function.
    SCAR Code:
    function FunctionNameHere: TypeOfVariableItReturns;
    begin
    //What you want the function to do goes here
    result:= Something;
    end;

    Ahhh! What's all that do? I'll cover it here.

    FunctionNameHere - The name of the function, just like a procedure.

    WhatToReturn
    - The type of value the function returns. EX: int, string, char, boolean, array of int, etc.

    Most commonly used variables [that are returned with Functions]
    - Boolean : probably one of the most commonly used variables to return, a boolean either returns true or false.
    SCAR Code:
    boolean1:= true;
    boolean2:= false;

    - String : this returns any text of any type. Strings are essentially an array of char, as described below.
    SCAR Code:
    string1:= 'hello everybody 67!';
    char1:= string1[10];

    result:= - The result is the value you want the function to return. So lets say you want the function to return 2 times 2. You could do result:= 2 * 2. I'll cover more as I go on.

    So, lets get down and dirty (with functions, of course). Now that I've covered all the terms, lets get an example.
    SCAR Code:
    function MyName: string;
    begin
     result:= 'Kik';
    end;

    Now lets implement that into a script, much like we did with a procedure. However, this time, because the function MyName returns a string, we can't just simply declare it, because it doesn't know what do with it. So we have to add a WriteLn to it.
    SCAR Code:
    program new;

    function MyName: string;
    begin
      result:= 'Kik';
    end;

    begin
      Writeln(MyName);
    end.

    But why not just do Writeln('Kik')? Well, the example above is an extremely simple one. functions and procedures can hold many values, but I'm not going into that depth yet. Here's another example that shows the power of functions, especially with a boolean.
    SCAR Code:
    program new;
    var I: integer;

    function FindTheColor: boolean;
    var mx, my, x, y: integer;
    begin
      GetMousePos(mx, my);  //(mx, my) is the position of your mouse
       //See my tutorial in my signature for more info on basic Color Finding.
      if FindColor(x, y, 255, mx - 10, my - 10, mx + 10, my + 10) then
      result:= true;  
    end;

    begin
      repeat
        wait(300);
      until(FindTheColor);
    end.

    In this script, I use the funciton FindTheColor. For those of you new to booleans, a boolean is a variable that is either true or false. The function gets the Mouse Position, (mx, my) and if it finds the color 255 (basic red) in a 20 by 20 box around the mouse then the function returns true. In the mainloop, it repeats wait(300) until FindTheColor [is true].

    NOTE: When declaring variables inside a procedure (mx, my, x, y), the variables can only be used inside that procedure. To SCAR, mx, my, x, and y don't exist outside this procedure.

    4.0 ) Declaring Variables within Procedures and Functions (What?)

    Declaring variables within procedures and functions? What's that? Well, that's the best way I could think of to say how to use variables within a procedure. So, lets say you want to make a procedure that writes out something (because you don't want to just use plain WriteLn), but you want to write something different every time. How do we let SCAR know what to write? Heres an example.
    SCAR Code:
    program new;

    procedure WriteSomething(Text: string);
    begin
      Writeln(Text);
    end;

    begin
      WriteSomething('Kik is teh awesome!');
    end.

    As you can see, in the line that we declare the procedure we also declare the variable Text as a string. We then tell SCAR that this procedure should write out whatever Text happens to be. 'Var' is not needed when declaring a variable in a procedure as it is already assumed by SCAR.

    Another one of my favorite ways of using a procedure is for Debugging (Having SCAR right out when commands are followed through IE: 'Tree Found', 'Dropped Inventory')
    SCAR Code:
    program new;
    var DoIDebug: boolean;

    procedure Debug(Write: string);
    begin
      if (DoIDebug = True) then
      Writeln(Write);
    end;

    begin
      DoIDebug:= True;
      Debug('Hi all');
      DoIDebug:= False;
      Debug('Where did this text go?');
    end.
    As you can see, Debug will only write out text if DoIDebug is true.


    Declaring a function with a variable is pretty much the same as in procedures. Here's another example for you that uses integers.

    SCAR Code:
    program new;

    function Subtract(Num1: integer; Num2: integer): integer; //can also be said (Num1, Num2: integer)
    begin
      result:= Num1 - Num2;
    end;

    begin
      Writeln(IntToStr(Subtract(5, 2)));
    end.
    When writing an integer, you must use IntToStr(..) or SCAR will give you an error.

    Well, you've reached the end of my tutorial. I hope this one actually gets more than 3 posts like my Color Finding tut, but, hey, at least it helped you! If there's anything I missed, needs clarification, things to add, etc, just send me a PM.
    I dunno, those asians are pretty difficult to out-auto, legend has it they don't need sleep or food...~tim0suprem0
    Activity is on the decline - school's got me
    Check out my tutorial[s] on Color Finding!||Procedures and Functions!

  2. #2
    Join Date
    Jun 2007
    Location
    Hell Stream
    Posts
    584
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Nice, although there are numbers of tut that talk about the simple of procedure and function but you have make it stand out of the cloud and the explanation was good and clear and some example of yours. Nice

  3. #3
    Join Date
    Sep 2007
    Posts
    415
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    very nice tut, helped me with functions a lot, Thanx m8
    Quote Originally Posted by That guy that wrote forefeathers
    <munklez>haha im too lazy, girls annoy me
    <munklez> they always wanna like, do stuff
    <munklez> and i just wanna program
    <munklez> and they always take all my money

  4. #4
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,553
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Gj

  5. #5
    Join Date
    Jul 2007
    Location
    America
    Posts
    421
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks... I just did a search and found like 5 other tutorials on making procedures/functions... another tutorial can never hurt
    I dunno, those asians are pretty difficult to out-auto, legend has it they don't need sleep or food...~tim0suprem0
    Activity is on the decline - school's got me
    Check out my tutorial[s] on Color Finding!||Procedures and Functions!

  6. #6
    Join Date
    Oct 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    pretty good guide, helps noobs alot

  7. #7
    Join Date
    Nov 2007
    Location
    England
    Posts
    10
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I just thought id say "wow" on both your tuts they really brought things back to basics and helped me figure both colour finding and functions out.

    P.S. Your work has not gone in vain.
    Activity decline-school's found me
    http://i161.photobucket.com/albums/t...ife/imapen.png
    Simply the BEST tutout there
    http://www.srl-forums.com/forum/scar-srl-tutorial-t18739.html

  8. #8
    Join Date
    Aug 2007
    Location
    Hawaii
    Posts
    3,880
    Mentioned
    7 Post(s)
    Quoted
    152 Post(s)

    Default

    Thx man appreciate your tut. Helped me understand functions =p.
    Faith is an oasis in the heart which will never be reached by the caravan of thinking.

  9. #9
    Join Date
    Nov 2007
    Location
    USA
    Posts
    35
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Good tut. Clarified the difference between functions and procedures. Will be helpful in creating scripts. Thanks.

  10. #10
    Join Date
    Jul 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks, this solved a lot of my unanswered questions.

  11. #11
    Join Date
    Jun 2007
    Posts
    1,312
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Good job, and happy birthday.
    Active only during the Summer...

  12. #12
    Join Date
    Apr 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Sweet! Helped me alot.

  13. #13
    Join Date
    Apr 2007
    Location
    Michigan -.-
    Posts
    1,357
    Mentioned
    2 Post(s)
    Quoted
    4 Post(s)

    Default

    very clear, good work!

    i know that i didnt know how to use functions my first few months scripting because there wasnt a good tut on it

    nicely done
    METAL HEAD FOR LIFE!!!

  14. #14
    Join Date
    May 2007
    Posts
    5
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by gerauchert View Post
    very clear, good work!

    i know that i didnt know how to use functions my first few months scripting because there wasnt a good tut on it

    nicely done
    Ditto. I have yet to find a good tut on functions.
    Until this, that is. It definately helped clear some things up that I had never learned with functions
    Thanks alot.

  15. #15
    Join Date
    Dec 2006
    Location
    SC
    Posts
    692
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by exps35 View Post
    Ditto. I have yet to find a good tut on functions.
    Until this, that is. It definately helped clear some things up that I had never learned with functions
    Thanks alot.
    Create Your Own Commands

    I should have put that one in the Intermediate Section, since it is a little bit more than just plain functions.

    BTW, nice tutorial, Kik. You used adequate examples and explained it in a simplistic, but helpful way.

  16. #16
    Join Date
    Jan 2008
    Posts
    89
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    OK a nooby quesiton but here it goes.

    if (FindDTM(LobsterCage,Dx,Dy,MIX1,MIY1,MIX2,MIY2))th en

    in that line, do i gotta replace the mix1, etc stuff with the box of the inventory that i wish it to search? or leave it like that. thanks in advance

    yours,
    prince

  17. #17
    Join Date
    Nov 2007
    Location
    Chile
    Posts
    1,901
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by prince View Post
    OK a nooby quesiton but here it goes.

    if (FindDTM(LobsterCage,Dx,Dy,MIX1,MIY1,MIX2,MIY2))th en

    in that line, do i gotta replace the mix1, etc stuff with the box of the inventory that i wish it to search? or leave it like that. thanks in advance

    yours,
    prince
    You can leave it at MIX1 becouse DTMs looks for the exact color, size, etc...


Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Procedure TypeSendRandom & Procedure AutoResponder!
    By Ultra in forum Research & Development Lounge
    Replies: 12
    Last Post: 01-08-2008, 07:04 PM
  2. Replies: 8
    Last Post: 05-24-2007, 11:57 PM
  3. Procedure that calls random procedure?
    By Secet in forum OSR Help
    Replies: 2
    Last Post: 03-03-2007, 03:56 PM

Posting Permissions

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