por endereço e por memoria
galera, qual a diferença de variavel por endereço e por memoria?!
Eu sei que um se poe var na frente e o outro nao(dentro da function, procedure)
POderiam me explicar?!
Obrigado!!!
Eu sei que um se poe var na frente e o outro nao(dentro da function, procedure)
POderiam me explicar?!
Obrigado!!!
Tremonti
Curtidas 0
Respostas
Ipc$
11/10/2004
Endereços nada mais são que ponteiros pq eles apontam para a variável que está na memória. Por ex:
var i:Integer; pi:Pointer;
begin
i := 123; // variável de memória
pi := @i; // endereço da memória onde está a variável i
end;
var i:Integer; pi:Pointer;
begin
i := 123; // variável de memória
pi := @i; // endereço da memória onde está a variável i
end;
GOSTEI 0
Ehvasc
11/10/2004
Olá caro colega,
Quando na assinatura/implementação de um método usa-se parametros, os mesmo podem ser passados por valor ou por referencia:
Quando se passa por referencia (usando o var antes do nome do parametro) se dentro do método ocorrer alguma modificação no valor desse parametro, no final do método a variável passada na chamada do método também terá seu valor alterado. Por Exemplo
procedure SOMA (a,b:Integer; var s:integer);
begin
s := a + b;
end;
procedure SOMA2 (a,b:integer; s:integer);
begin
s := a+b;
end;
procedure Tform1.btnTesteClick(Sender:TObject);
var x,y,z : integer;
begin
x := 3;
y := 5;
SOMA(x,y,z);
ShowMessage(inttostr(z)); //Retorna o valor 8
x := 10;
y := 5;
SOMA2(x,y,z);
ShowMessage(inttostr(z)); //Retorna o valor 8
{Pois o valor de z não foi alterado porque foi passado por valor}
end;
Espero ter ajudado. Boa Sorte. QUalquer dúvida é só entrar em contato
Quando na assinatura/implementação de um método usa-se parametros, os mesmo podem ser passados por valor ou por referencia:
Quando se passa por referencia (usando o var antes do nome do parametro) se dentro do método ocorrer alguma modificação no valor desse parametro, no final do método a variável passada na chamada do método também terá seu valor alterado. Por Exemplo
procedure SOMA (a,b:Integer; var s:integer);
begin
s := a + b;
end;
procedure SOMA2 (a,b:integer; s:integer);
begin
s := a+b;
end;
procedure Tform1.btnTesteClick(Sender:TObject);
var x,y,z : integer;
begin
x := 3;
y := 5;
SOMA(x,y,z);
ShowMessage(inttostr(z)); //Retorna o valor 8
x := 10;
y := 5;
SOMA2(x,y,z);
ShowMessage(inttostr(z)); //Retorna o valor 8
{Pois o valor de z não foi alterado porque foi passado por valor}
end;
Espero ter ajudado. Boa Sorte. QUalquer dúvida é só entrar em contato
GOSTEI 0
Tremonti
11/10/2004
POutz cara, vc explicou bem, mas nao entendi muito nao...
Nao sei quando usar cada uma delas...
valew mano
Abraços
Nao sei quando usar cada uma delas...
valew mano
Abraços
GOSTEI 0
Paulo_amorim
11/10/2004
Olá
[quote:c6cb5f5904=´Help do Delphi´]Most parameters are either value parameters (the default) or variable (var) parameters. Value parameters are passed by value, while variable parameters are passed by reference. To see what this means, consider the following functions.
function DoubleByValue(X: Integer): Integer; // X is a value parameter
begin
X := X * 2;
Result := X;
end;
function DoubleByRef(var X: Integer): Integer; // X is a variable parameter
begin
X := X * 2;
Result := X;
end;
These functions return the same result, but only the second one—DoubleByRef—can change the value of a variable passed to it. Suppose we call the functions like this:
var
I, J, V, W: Integer;
begin
I := 4;
V := 4;
J := DoubleByValue(I); // J = 8, I = 4
W := DoubleByRef(V); // W = 8, V = 8
end;
After this code executes, the variable I, which was passed to DoubleByValue, has the same value we initially assigned to it. But the variable V, which was passed to DoubleByRef, has a different value.
A value parameter acts like a local variable that gets initialized to the value passed in the procedure or function call. If you pass a variable as a value parameter, the procedure or function creates a copy of it; changes made to the copy have no effect on the original variable and are lost when program execution returns to the caller.
A variable parameter, on the other hand, acts like a pointer rather than a copy. Changes made to the parameter within the body of a function or procedure persist after program execution returns to the caller and the parameter name itself has gone out of scope.
Even if the same variable is passed in two or more var parameters, no copies are made. This is illustrated in the following example.
procedure AddOne(var X, Y: Integer);
begin
X := X + 1;
Y := Y + 1;
end;
var I: Integer;
begin
I := 1;
AddOne(I, I);
end;
After this code executes, the value of I is 3.[/quote:c6cb5f5904]
[quote:c6cb5f5904=´Help do Delphi´]Most parameters are either value parameters (the default) or variable (var) parameters. Value parameters are passed by value, while variable parameters are passed by reference. To see what this means, consider the following functions.
function DoubleByValue(X: Integer): Integer; // X is a value parameter
begin
X := X * 2;
Result := X;
end;
function DoubleByRef(var X: Integer): Integer; // X is a variable parameter
begin
X := X * 2;
Result := X;
end;
These functions return the same result, but only the second one—DoubleByRef—can change the value of a variable passed to it. Suppose we call the functions like this:
var
I, J, V, W: Integer;
begin
I := 4;
V := 4;
J := DoubleByValue(I); // J = 8, I = 4
W := DoubleByRef(V); // W = 8, V = 8
end;
After this code executes, the variable I, which was passed to DoubleByValue, has the same value we initially assigned to it. But the variable V, which was passed to DoubleByRef, has a different value.
A value parameter acts like a local variable that gets initialized to the value passed in the procedure or function call. If you pass a variable as a value parameter, the procedure or function creates a copy of it; changes made to the copy have no effect on the original variable and are lost when program execution returns to the caller.
A variable parameter, on the other hand, acts like a pointer rather than a copy. Changes made to the parameter within the body of a function or procedure persist after program execution returns to the caller and the parameter name itself has gone out of scope.
Even if the same variable is passed in two or more var parameters, no copies are made. This is illustrated in the following example.
procedure AddOne(var X, Y: Integer);
begin
X := X + 1;
Y := Y + 1;
end;
var I: Integer;
begin
I := 1;
AddOne(I, I);
end;
After this code executes, the value of I is 3.[/quote:c6cb5f5904]
GOSTEI 0
Ipc$
11/10/2004
Para vc entender melhor, pegue o último trecho do código:
Ele está passando o ponteiro de I p/ X e p/ Y. Na primeira instrução:
X := X+1, I valerá 2. Na segunda instrução Y := Y+1, I valerá 3.
Se os parâmetros não fossem [b:dd1ba48088]var[/b:dd1ba48088] X,Y:Integer - A procedure AddOne não serviria para nada, ou seja, vc passaria AddOne(I,I) e a variável I continuaria valendo 1.
procedure AddOne(var X, Y: Integer); begin X := X + 1; Y := Y + 1; end; var I: Integer; begin I := 1; AddOne(I, I); end; After this code executes, the value of I is 3.
Ele está passando o ponteiro de I p/ X e p/ Y. Na primeira instrução:
X := X+1, I valerá 2. Na segunda instrução Y := Y+1, I valerá 3.
Se os parâmetros não fossem [b:dd1ba48088]var[/b:dd1ba48088] X,Y:Integer - A procedure AddOne não serviria para nada, ou seja, vc passaria AddOne(I,I) e a variável I continuaria valendo 1.
GOSTEI 0