Params by value / reference - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Params by value / reference

dve83dve83 Posts: 30Member
Why does the Editbox not react the same as an Integer value when I send it to the procedure as a parameter by value? WHen sending a parameter by value, its not suppose to change outside the procedure, however when sending a component -> it does? see code. I cant get my head around it. thx so long! note:code was written in notepad, excuse any syntax errors.

[code]
unit unitname;
interface
uses xxx,zzz;

type
TForm1 = Class(TForm)
Edit1:TEdit
private
Value:integer;
procedure CHangeInt(aValue:integer);
procedure ChangeIntVAR(var avalue:integer);
procedure ChangeEdit(aEdit:TEdit);
procedure CHangeEditVAR(aEdit.TEdit);
public
end;

var
Form1: TForm1;

procedure DoStuff();

implementation

procedure DoStuff();
begin
Value := 0;
Edit1.Text := '0';

ChangeInt(Value);ShowMessage(inttostr(value)); //result = 0
CHangeintVAR(value);ShowMessage(inttostr(value));//result =2
ChangeEdit(Edit1);ShowMessage(Edit1.Text); //result='1'; <===WHY?!!?
ChangeEditVAR(Edit1);ShowMessage(Edit1.Text); //Result='2';

end;

procedure TForm1.CHangeInt(aValue:integer);
begin
aValue:=1;
end;

procedure TForm1.ChangeIntVAR(var avalue:integer);
begin
aValue:=2;
end;

procedure TForm1.ChangeEdit(aEdit:TEdit);
begin
aEdit.Text := '1';
end;

procedure TForm1.CHangeEditVAR(var aEdit.TEdit);
begin
aEdit.Text := '2';
end;
[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : Why does the Editbox not react the same as an Integer value when I send it to the procedure as a parameter by value? WHen sending a parameter by value, its not suppose to change outside the procedure, however when sending a component -> it does? see code. I cant get my head around it. thx so long! note:code was written in notepad, excuse any syntax errors.
    :
    : [code]
    : unit unitname;
    : interface
    : uses xxx,zzz;
    :
    : type
    : TForm1 = Class(TForm)
    : Edit1:TEdit
    : private
    : Value:integer;
    : procedure CHangeInt(aValue:integer);
    : procedure ChangeIntVAR(var avalue:integer);
    : procedure ChangeEdit(aEdit:TEdit);
    : procedure CHangeEditVAR(aEdit.TEdit);
    : public
    : end;
    :
    : var
    : Form1: TForm1;
    :
    : procedure DoStuff();
    :
    : implementation
    :
    : procedure DoStuff();
    : begin
    : Value := 0;
    : Edit1.Text := '0';
    :
    : ChangeInt(Value);ShowMessage(inttostr(value)); //result = 0
    : CHangeintVAR(value);ShowMessage(inttostr(value));//result =2
    : ChangeEdit(Edit1);ShowMessage(Edit1.Text); //result='1'; <===WHY?!!?
    : ChangeEditVAR(Edit1);ShowMessage(Edit1.Text); //Result='2';
    :
    : end;
    :
    : procedure TForm1.CHangeInt(aValue:integer);
    : begin
    : aValue:=1;
    : end;
    :
    : procedure TForm1.ChangeIntVAR(var avalue:integer);
    : begin
    : aValue:=2;
    : end;
    :
    : procedure TForm1.ChangeEdit(aEdit:TEdit);
    : begin
    : aEdit.Text := '1';
    : end;
    :
    : procedure TForm1.CHangeEditVAR(var aEdit.TEdit);
    : begin
    : aEdit.Text := '2';
    : end;
    : [/code]
    :
    Objects are passed as pointers. This means that the aEdit is a pointer, which holds several other values. If you change a property then the object pointer isn't changed, only a part of the memory to which it points. Since that memory part is located in the heap instead of the procedure stack, it will be changed globally.
    In case of simple types (integers, floats, strings, etc), the value is copied from the heap into the procedure stack. If that value is changed, the heap copy isn't updated, unless the parameter was variable.
  • dve83dve83 Posts: 30Member
    : : Why does the Editbox not react the same as an Integer value when I send it to the procedure as a parameter by value? WHen sending a parameter by value, its not suppose to change outside the procedure, however when sending a component -> it does? see code. I cant get my head around it. thx so long! note:code was written in notepad, excuse any syntax errors.
    : :
    : : [code]
    : : unit unitname;
    : : interface
    : : uses xxx,zzz;
    : :
    : : type
    : : TForm1 = Class(TForm)
    : : Edit1:TEdit
    : : private
    : : Value:integer;
    : : procedure CHangeInt(aValue:integer);
    : : procedure ChangeIntVAR(var avalue:integer);
    : : procedure ChangeEdit(aEdit:TEdit);
    : : procedure CHangeEditVAR(aEdit.TEdit);
    : : public
    : : end;
    : :
    : : var
    : : Form1: TForm1;
    : :
    : : procedure DoStuff();
    : :
    : : implementation
    : :
    : : procedure DoStuff();
    : : begin
    : : Value := 0;
    : : Edit1.Text := '0';
    : :
    : : ChangeInt(Value);ShowMessage(inttostr(value)); //result = 0
    : : CHangeintVAR(value);ShowMessage(inttostr(value));//result =2
    : : ChangeEdit(Edit1);ShowMessage(Edit1.Text); //result='1'; <===WHY?!!?
    : : ChangeEditVAR(Edit1);ShowMessage(Edit1.Text); //Result='2';
    : :
    : : end;
    : :
    : : procedure TForm1.CHangeInt(aValue:integer);
    : : begin
    : : aValue:=1;
    : : end;
    : :
    : : procedure TForm1.ChangeIntVAR(var avalue:integer);
    : : begin
    : : aValue:=2;
    : : end;
    : :
    : : procedure TForm1.ChangeEdit(aEdit:TEdit);
    : : begin
    : : aEdit.Text := '1';
    : : end;
    : :
    : : procedure TForm1.CHangeEditVAR(var aEdit.TEdit);
    : : begin
    : : aEdit.Text := '2';
    : : end;
    : : [/code]
    : :
    : Objects are passed as pointers. This means that the aEdit is a pointer, which holds several other values. If you change a property then the object pointer isn't changed, only a part of the memory to which it points. Since that memory part is located in the heap instead of the procedure stack, it will be changed globally.
    : In case of simple types (integers, floats, strings, etc), the value is copied from the heap into the procedure stack. If that value is changed, the heap copy isn't updated, unless the parameter was variable.
    :
    Thank you. I appreciate the reply & info.
    dve83
Sign In or Register to comment.