Results 1 to 11 of 11

Thread: Functions and procedures? Bullshit!

  1. #1
    Join Date
    Feb 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Quoted
    21 Post(s)

    Default Functions and procedures? Bullshit!

    Code:
    function add(a, b): integer;
    begin
      result := a + b;
    end;
    
    function function: integer;
    var
      a, b := integer;
    begin
      result := add(a, b);
    end;
    Code:
    procedure add(x, a, b)
    begin
      x := a + b;
    end;
    
    function procedure: integer;
    var
      a, b: integer;
    begin
      add(result, a, b);
    end;


    I may be late, but the differentiation from functions to procedures sucks.
    There is no fucking difference, if any, the difference or aesthethic, or even subjective,you might call something a function disguised as a procedure.
    And the aesthetic of the procedure as a void function is disgusting too, it is really inconsistent.
    Perhaps, the bad thing isn't the structure of the language but the scripters and programmers who use procedures as functions and add a result ( or more than one) variable to the parameters,which, scoping rules allow.
    Is there any other language that adopted this vomit of a structure?
    Perhaps someone with some knowledge on compiler theory can enlighten me into this.

  2. #2
    Join Date
    Jan 2009
    Location
    Turlock/LA, California
    Posts
    1,494
    Mentioned
    3 Post(s)
    Quoted
    66 Post(s)

    Default

    did u even attempt to compile that code? (hint, it wont compile)

  3. #3
    Join Date
    Sep 2012
    Location
    Here.
    Posts
    2,007
    Mentioned
    88 Post(s)
    Quoted
    1014 Post(s)

    Default

    Well besides the starter where your example code won't compile, I do get the gist of what you're curious about.

    It's not based in compiler theory so much as terminology. Methods and functions are technically different from a terminological perspective. Functions have a designated goal that returns a result and methods have a designated goal that does not return a result. Pascalscript simply takes that to a literal sense (and renamed 'method' to 'procedure' to be more in line with classic languages that typically refer to them as 'proc').

    When code is compiled down to bytecode, there IS a difference between a function and a procedure in how they are implemented. The difference is, most compilers recognize this difference and can automatically determine the difference. Pascalscript is a simple language and simply hasn't recognized that additional logic as built in auto-magic.

    Does that explain it well enough?

  4. #4
    Join Date
    Jan 2009
    Location
    Turlock/LA, California
    Posts
    1,494
    Mentioned
    3 Post(s)
    Quoted
    66 Post(s)

    Default

    and for a very brief history lesson:
    1. ALGOL was created in 1960 (influence by ALGOL58). used "proc"
    2. ALGOLW was eventually evolved from ALGOL60. used "procedure"
    3. Pascal was heavily influence by ALGOLW/ALGOL60. used "procedure"
    4. Pascalscript is a scripting language using Pascal. This is what Simba currently uses.

    so the idea of having something called a "procedures" goes back easily 50 years.

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

    Default

    :l What in the world.. I have never read such things before..

    IMO, well.. AFAIK, there is no such thing as a procedure :S

    A procedure is a "function" with no return type. Well I guess the same can be said backwards.. A function is a procedure with a return value.


    C++ Code:
    void SomeProcedure()
    {
    }

    int SomeIntegerFunction()
    {
        return 0;
    }

    template<typename T>
    T SomeTemplateFunction()
    {
       return SomeTValue;
    }

    Java Code:
    public void SomeProcedure()
    {
    }

    public int SomeIntFunction()
    {
       return 0;
    }

    public <T> T SomeGenericFunction()
    {
       return SomeTValue;
    }

    Pascal Code:
    procedure SomeVoidFunction;
    begin
    end;

    function SomeIntegerFunction: Integer;
    begin
      result := 0;
    end;

    ASM Code:
    section '.data' data readable writeable
    Index dd 0

    Function:
       push ebp
       mov ebp, esp
       mov eax, [ebp + 8]

       push eax
       call [printf]
       add esp, 8

       mov esp, ebp
       pop ebp
    ret


    main:
       push ebp    
       mov ebp, esp   ;Save stack pointer if you decide to return a value.. otherwise forget it..

       push eax
       call Function
       add esp, 4

       mov esp, ebp
       pop ebx       ;Restore stack pointer.. Only if you saved it in the first place..
     
       mov eax, 0  ;Don't have to return 0. Can do nothing depending on the machine you're on..
    ret
    Last edited by Brandon; 04-12-2013 at 03:29 AM.

  6. #6
    Join Date
    Feb 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Quoted
    21 Post(s)

    Default

    Quote Originally Posted by x[Warrior]x3500 View Post
    and for a very brief history lesson:
    1. ALGOL was created in 1960 (influence by ALGOL58). used "proc"
    2. ALGOLW was eventually evolved from ALGOL60. used "procedure"
    3. Pascal was heavily influence by ALGOLW/ALGOL60. used "procedure"
    4. Pascalscript is a scripting language using Pascal. This is what Simba currently uses.

    so the idea of having something called a "procedures" goes back easily 50 years.
    Many practices were dropped since the 50, namely, the "procedure" keyword.

    Quote Originally Posted by Kevin View Post
    Well besides the starter where your example code won't compile, I do get the gist of what you're curious about.

    It's not based in compiler theory so much as terminology. Methods and functions are technically different from a terminological perspective. Functions have a designated goal that returns a result and methods have a designated goal that does not return a result. Pascalscript simply takes that to a literal sense (and renamed 'method' to 'procedure' to be more in line with classic languages that typically refer to them as 'proc').

    When code is compiled down to bytecode, there IS a difference between a function and a procedure in how they are implemented. The difference is, most compilers recognize this difference and can automatically determine the difference. Pascalscript is a simple language and simply hasn't recognized that additional logic as built in auto-magic.

    Does that explain it well enough?
    Shame on my code, I forgot to define the paramters, it was never intended to compile, though, the function's identifiers are keywords too

    Could you tell me what is the difference between a procedure with result as an argument and a function?

    Quote Originally Posted by Brandon View Post
    :l What in the world.. I have never read such things before..

    IMO, well.. AFAIK, there is no such thing as a procedure :S

    A procedure is a "function" with no return type. Well I guess the same can be said backwards.. A function is a procedure with a return value.


    C++ Code:
    void SomeProcedure()
    {
    }

    int SomeIntegerFunction()
    {
        return 0;
    }

    template<typename T>
    T SomeTemplateFunction()
    {
       return SomeTValue;
    }

    Java Code:
    public void SomeProcedure()
    {
    }

    public int SomeIntFunction()
    {
       return 0;
    }

    public <T> T SomeGenericFunction()
    {
       return SomeTValue;
    }

    Pascal Code:
    procedure SomeVoidFunction;
    begin
    end;

    function SomeIntegerFunction: Integer;
    begin
      result := 0;
    end;

    ASM Code:
    section '.data' data readable writeable
    Index dd 0

    Function:
       push ebp
       mov ebp, esp
       mov eax, [ebp + 8]

       push eax
       call [printf]
       add esp, 8

       mov esp, ebp
       pop ebp
    ret


    main:
       push ebp    
       mov ebp, esp   ;Save stack pointer if you decide to return a value.. otherwise forget it..

       push eax
       call Function
       add esp, 4

       mov esp, ebp
       pop ebx       ;Restore stack pointer.. Only if you saved it in the first place..
     
       mov eax, 0  ;Don't have to return 0. Can do nothing depending on the machine you're on..
    ret
    C/C++ and Java syntax is definitely neater and more beautiful for me.




    Don't hate me for hating pascal.

  7. #7
    Join Date
    Feb 2012
    Location
    Discord
    Posts
    3,114
    Mentioned
    37 Post(s)
    Quoted
    538 Post(s)

    Default

    Quote Originally Posted by pija View Post
    Many practices were dropped since the 50, namely, the "procedure" keyword.


    Shame on my code, I forgot to define the paramters, it was never intended to compile, though, the function's identifiers are keywords too

    Could you tell me what is the difference between a procedure with result as an argument and a function?



    C/C++ and Java syntax is definitely neater and more beautiful for me.




    Don't hate me for hating pascal.
    @BMWxi remind you of something?

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

    Default

    I don't hate you for hating Pascal as I don't exactly like it either but knowing it is a good thing as it does teach the constructs of most languages fairly easily. It's just that it is a bit too wordy for my style.

    Quote Originally Posted by pija View Post
    Could you tell me what is the difference between a procedure with result as an argument and a function?
    Why yes I can

    C++ Code:
    void SomeProcedure(int &Value)  //Procedure with return type as Parameter. Passed by Reference.
    {
        Value = 0;
    }

    Pascal Code:
    Procedure SomeProcedure(var Value: Integer);  //Pass by Reference as well..
    begin
       Value := 0;
    end;


    The difference between a Procedure with a value passed by reference vs a function that returns a Value is "Copying".

    Take this for example:

    C++ Code:
    std::vector<int> SomeFunction()
    {
        return {1, 2, 3, 4, 5};  //same as std::vector<int> Result = {1, 2, 3, 4, 5}; return Result; Notice the temporary being created?
    }

    int main()
    {
        std::vector<int> SomeValue = SomeFunction(); //At this line, SomeFunction creates a vector.. When assigned, the value is COPIED to SomeValue.
        //In the end, two vectors were created. The temporary one inside the function, and the one we created to be assigned to.
    }

    //Now see this:
    void SomeProcedure(std::vector<int> &Value)
    {
       Value = {1, 2, 3, 4, 5};
    }

    int main()
    {
       std::vector<int> Value;  //One vector is created..
       SomeProcedure(Value);    //Modifies that vector to hold the returned values.. No copying done.
    }


    Pascal Code:
    Function SomeFunction: TIntegerArray;
    begin
       SetLength(Result, 5);
       Result := [1, 2, 3, 4, 5];
    end;

    //main.
    var
      Value: TIntegerArray;
    begin
      Value := SomeFunction;  //SomeFunction Creates an array of 5 values. The assignment operator copies that array to "Value" variable.
    end.



    Procedure SomeProcedure(var Value: TIntegerArray)
    begin
       SetLength(Value, 5);
       Value := [1, 2, 3, 4, 5]; //Not sure if I can do that.. Might have to do Value[0] = 1; Value[1] = 2; etc..
    end;

    //main.
    var
       Value: TIntegerArray;
    begin
       SomeProcedure(Value);  //No copying done.. Only modifying..
    end.

    The above pascal code may not be compilable.. See my inline-comment. Anyway, you get the idea.

    Functions that return a result usually "Copy" whereas a function taking a result by reference simply modifies.

    You use return types where you simply do not want to modify the original and pass by reference where you want to save memory/speed.

    C++x11 gets rid of this copying with R-Value reference where it uses std::move on a Temporary object such that no copying is done, but rather the object/resource can be moved to the returned value rather than copied.

    MoveConstructs and CopyConstructors.

    I'm unaware of this being done in pascal but that's what PassByReference does. Java has PassByValueReference. It is not the same. PassByValueReference simply passes a "Copy" of a "Reference" such that the original Reference is never lost but the object can be modified through accessor/mutator methods {get/set} but the actual reference itself may not be modified.
    Last edited by Brandon; 04-12-2013 at 07:55 PM.

  9. #9
    Join Date
    Feb 2013
    Posts
    38
    Mentioned
    0 Post(s)
    Quoted
    21 Post(s)

    Default

    Ah, I see, so the difference between "function somefunction:integer" and "procedure someprocedure(someresult:integer)"
    would be the scoping for the "result" variable.
    I feel smarter. Thanks.

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

    Default

    Quote Originally Posted by The Killer View Post
    @BMWxi remind you of something?
    I guess beauty is in the eye of the beholder

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

    Default

    Quote Originally Posted by pija View Post
    Ah, I see, so the difference between "function somefunction:integer" and "procedure someprocedure(someresult:integer)"
    would be the scoping for the "result" variable.
    I feel smarter. Thanks.
    Also note that the high quantity of procedures that should be functions exported by simba is caused by a bug in pascalscript. It doesn't always free complicated types from the scope. Therefor a procedure is more safe. With lape this is fixed.
    Working on: Tithe Farmer

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
  •