It's still passed by value. You'd have to explicitly pass the integers as pointers by using "var". Then the "X := X + 10;" line would modify the source variable. Otherwise just the local copy of the variable is modified.
It's still passed by value. You'd have to explicitly pass the integers as pointers by using "var". Then the "X := X + 10;" line would modify the source variable. Otherwise just the local copy of the variable is modified.
He's right. Compare these:Originally Posted by Setharian
[pascal]
function MyPoint(X, Y: Integer): TMyPoint;
begin
X := X + 10;
Result.X := X;
Result.Y := Y;
end;
var
X, Y: Integer;
begin
X := 10;
Y := 20;
MyPoint(X, Y);
writeln(X);
readln;
end.
[/pascal]
[pascal]
function MyPoint(var X, Y: Integer): TMyPoint;
begin
X := X + 10;
Result.X := X;
Result.Y := Y;
end;
var
X, Y: Integer;
begin
X := 10;
Y := 20;
MyPoint(X, Y);
writeln(X);
readln;
end.
[/pascal]
In the first case, X after passing to MyPoint still equals 10. But in the latter, X is passed as a reference, thus it equals 20.
You can also use Assembler to boost up your program:
[pascal]
function MyPoint(X, Y: Integer): TMyPoint;
// EAX contains address of X
// EDX contains address of Y
// ECX contains the result
asm
mov [ECX], EAX
mov [ECX+4], EDX
end;
[/pascal]
But I doubt you'll gain any speed using this.
For anything with more than 4bytes in sizeOriginally Posted by cronodragon
From brazil (:
Pascal pownz!
Hmm, a method type (procedure of object) has 8 bytes and I haven't seen any tip about passing it with Const. What the Delphi Help says is that const should be used with structure types.Originally Posted by arthurprs
Okay... thanx for the info... realy usefull
Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.
Maybe its an "exeption" (not sure about this word)Originally Posted by cronodragon
From brazil (:
Pascal pownz!
What const does is simply declare it as a constant in the function. The function can thus not alter it on syntax level, and the compiler then knows that all variables can be passed by reference. This is only a good thing if the parameter is very big, like a string or a big record
With simple types you don't need it
Peregrinus, expectavi pedes meos in cymbalis
Nullus norvegicorum sole urinat
Bookmarks