Results 1 to 9 of 9

Thread: Function vs Procedure?

  1. #1
    Join Date
    Apr 2015
    Location
    FireFox
    Posts
    528
    Mentioned
    10 Post(s)
    Quoted
    227 Post(s)

    Default Function vs Procedure?

    What's the difference between the two? What defines a procedure and what defines a function? What's the reason for putting (examples) : Boolean; and (theATPA : T2DPointArray) : T2DPointArray; at the end of funtions, do they serve a purpose?

    My apologies if these are basic but I have been wondering about them for a while now and can't stand not knowing,
    Cheers.
    Scripting with ogLib

  2. #2
    Join Date
    Mar 2015
    Posts
    438
    Mentioned
    21 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by srlMW View Post
    What's the difference between the two? What defines a procedure and what defines a function? What's the reason for putting (examples) : Boolean; and (theATPA : T2DPointArray) : T2DPointArray; at the end of funtions, do they serve a purpose?

    My apologies if these are basic but I have been wondering about them for a while now and can't stand not knowing,
    Cheers.
    Someone else will probably give a much better and more accurate explanation - but this is how I see it. A function is something that can be used within a procedure (generally you don't call on it directly) while a procedure is a dedicated process - hence procedure which is used in your main loop.

    Edit: I suppose a good way to look at it - if you look at the includes they consist of functions, ultimately you can do (for OGL) dialogue.hasDialogue - consisting of a function on the back-end opposed to manually putting in all of that work in multiple places. Instead you use dialogue.hasDialogue to make it look cleaner and easier to read opposed to having 15 extra lines each time you use it.
    Last edited by Clutch; 07-15-2015 at 01:04 PM.

  3. #3
    Join Date
    May 2006
    Location
    Belgium
    Posts
    36
    Mentioned
    10 Post(s)
    Quoted
    10 Post(s)

    Default

    Functions return a value, procedures do not.

  4. #4
    Join Date
    Jun 2012
    Posts
    586
    Mentioned
    112 Post(s)
    Quoted
    296 Post(s)

    Default

    Functions return a value while procedures do not.

    Edit: beat by a second.

    The (example)s are parameters - information that both functions and procedures may or may not need/use.
    Last edited by Obscurity; 07-15-2015 at 01:06 PM.




    Skype: obscuritySRL@outlook.com

  5. #5
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    "procedure" will execute a block of code
    "function" will execute a block of code and allow you to return a variable of your choosing (Boolean, T2DPointArray, etc)

    i.e function testfunction() : Boolean; will return a boolean variable, procedure testProcedure(); will not return anything.

    A well structured script will have quite a lot of functions and only a few procedures; the functions should do the calculation and whatnot, whereas the procedures will initialize and run the script

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

    Default

    The difference is that a function returns some sort of variable as the result. The type of the return variable is stated after the function name and parameters.

    E.g. The following function returns an integer. You use the result to assign the return variable:



    Some more examples:

    Simba Code:
    function howMuch(): Integer;
    begin
      result := 10;
    end;

    function doubleIt(i: Integer): Integer;
    begin
      result := i * 2;
    end;

    begin
      writeLn(howMuch()); // howMuch() is evaluated, and the the result is printed

      writeLn(doubleIt(howMuch())); // Will print 20
    end.

  7. #7
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    IMO, it doesn't matter the difference.

    Simba Code:
    Procedure Bar(var returnValue: Integer);
    begin
      returnValue := 1024; //returns 1024 by modifying the parameter.
    end;

    //is the same as:

    Function Foo(): Integer;
    begin
      result := 1024; //returns 1024 as a result of the function call.
    end;

    //is the same as:

    Procedure Meh(returnValue: ^Integer);
    begin
      returnValue^ := 1024; //returns 1024 by modifying the parameter pointed to.
    end;

    var
      someVal: Integer;
    begin
      someVal := Foo(); //someVal = the result of the function.

      Bar(someVal); //someVal = the value the procedure assigned it via reference parameter.

      Meh(@someVal); //someVal = the value the procedure assigned it via pointer parameter.
    end.
    I am Ggzz..
    Hackintosher

  8. #8
    Join Date
    Dec 2011
    Location
    East Coast, USA
    Posts
    4,231
    Mentioned
    112 Post(s)
    Quoted
    1869 Post(s)

    Default

    A function returns a value, a procedure just does what it's told.
    GitLab projects | Simba 1.4 | Find me on IRC or Discord | ScapeRune scripts | Come play bot ScapeRune!

    <BenLand100> we're just in the transitional phase where society reclassifies guns as Badâ„¢ before everyone gets laser pistols

  9. #9
    Join Date
    May 2014
    Posts
    633
    Mentioned
    8 Post(s)
    Quoted
    322 Post(s)

    Default

    Quote Originally Posted by Chaos-Energy View Post
    Functions return a value, procedures do not.
    This is the essence of the difference. It means you could write code like
    Simba Code:
    function1(function2(3,7), function3('Hello World'))
    if you desired (much simpler than or) instead of
    Simba Code:
    var
      f1,f2,f3 : integer; //f1,f2,f3 represent the 'return values' of the procedure
      proc2(f2,3,7);
      proc3(f3,'Hello World');
      proc1(f1,f2,f3);
    Where the procs do the exact same thing as the functions with their first argument being 'return value'.

    Usually when you don't care about return values or feedback values you just use procedures though. Example is a finder function should probably be a function (that returns a boolean or some type of coordinates) since you need to know whether you've found something or where it is.

    Meanwhile, your antiban should probably be a procedure since you don't really care if the antiban was 'successful', if it accidentally moused over prayer instead of thieving who really cares? You just care that it happens (on a similar vein you probably want mainloop to be a procedure since you just care that is run however often you need it to run and the return value would be kind of useless since the mainloop encapsulates everything important in your script anyways)

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
  •