SRL Archive - General

Functions and procedures? Bullshit!


pija

2013-04-11 15:42:38



function add(a, b): integer;
begin
result := a + b;
end;

function function: integer;
var
a, b := integer;
begin
result := add(a, b);
end;



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.

x[Warrior]x3500

2013-04-11 15:48:10

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

Kevin

2013-04-11 15:54:42

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?

x[Warrior]x3500

2013-04-11 16:07:44

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.

Brandon

2013-04-11 23:22:31

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



void SomeProcedure()
{
}

int SomeIntegerFunction()
{
return 0;
}

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


public void SomeProcedure()
{
}

public int SomeIntFunction()
{
return 0;
}

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


procedure SomeVoidFunction;
begin
end;

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


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

pija

2013-04-12 14:43:50

From: x[Warrior]x3500
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.

From: Kevin
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?

From: Brandon
: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.



void SomeProcedure()
{
}

int SomeIntegerFunction()
{
return 0;
}

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


public void SomeProcedure()
{
}

public int SomeIntFunction()
{
return 0;
}

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


procedure SomeVoidFunction;
begin
end;

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


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.

The Killer

2013-04-12 14:51:07

From: pija
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?

Brandon

2013-04-12 15:44:34

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.

From: pija

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


Why yes I can ;)


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


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:


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.
}



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.

pija

2013-04-12 16:03:40

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.

Ian

2013-04-12 16:10:30

From: The Killer
@BMWxi remind you of something?


I guess beauty is in the eye of the beholder ;)

masterBB

2013-04-13 05:12:40

From: pija
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.