arasındaki fark nedir?
Bana birkaç örnek verebilir misiniz lütfen?
Fonksiyonlara argüman aktarmanın bir yoludur. Referans ile geçmek, çağrılan fonksiyonun parametresinin, çağıran fonksiyonun geçtiği argüman ile aynı olacağı anlamına gelir (değer değil, kimlik - değişkenin kendisi). Değer ile geçmek, çağrılan fonksiyonun parametresinin, çağıran fonksiyonun geçirdiği argümanın bir kopyası olacağı anlamına gelir. Değer aynı olacaktır, ancak kimlik - değişken - farklıdır. Bu nedenle, çağrılan fonksiyon tarafından bir parametrede yapılan değişiklikler bir durumda geçirilen argümanı değiştirir ve diğer durumda sadece çağrılan fonksiyondaki parametrenin değerini değiştirir (ki bu sadece bir kopyadır). Acele edin:
ref
çağıran ve çağrılan fonksiyonda kullanılır). Jon Skeet'in bu konuda da güzel bir açıklaması var burada.Kodlar
Benim kullandığım dil C++ olduğu için burada onu kullanacağım
// passes a pointer (called reference in java) to an integer
void call_by_value(int *p) { // :1
p = NULL;
}
// passes an integer
void call_by_value(int p) { // :2
p = 42;
}
// passes an integer by reference
void call_by_reference(int & p) { // :3
p = 42;
}
// this is the java style of passing references. NULL is called "null" there.
void call_by_value_special(int *p) { // :4
*p = 10; // changes what p points to ("what p references" in java)
// only changes the value of the parameter, but *not* of
// the argument passed by the caller. thus it's pass-by-value:
p = NULL;
}
int main() {
int value = 10;
int * pointer = &value;
call_by_value(pointer); // :1
assert(pointer == &value); // pointer was copied
call_by_value(value); // :2
assert(value == 10); // value was copied
call_by_reference(value); // :3
assert(value == 42); // value was passed by reference
call_by_value_special(pointer); // :4
// pointer was copied but what pointer references was changed.
assert(value == 10 && pointer == &value);
}
Ve Java'da bir örnek zarar vermez:
class Example {
int value = 0;
// similar to :4 case in the c++ example
static void accept_reference(Example e) { // :1
e.value++; // will change the referenced object
e = null; // will only change the parameter
}
// similar to the :2 case in the c++ example
static void accept_primitive(int v) { // :2
v++; // will only change the parameter
}
public static void main(String... args) {
int value = 0;
Example ref = new Example(); // reference
// note what we pass is the reference, not the object. we can't
// pass objects. The reference is copied (pass-by-value).
accept_reference(ref); // :1
assert ref != null && ref.value == 1;
// the primitive int variable is copied
accept_primitive(value); // :2
assert value == 0;
}
}
Vikipedi
http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_value
http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference
Bu adam hemen hemen her şeyi anlatıyor:
İşte bir örnek:
#include <iostream>
void by_val(int arg) { arg += 2; }
void by_ref(int&arg) { arg += 2; }
int main()
{
int x = 0;
by_val(x); std::cout << x << std::endl; // prints 0
by_ref(x); std::cout << x << std::endl; // prints 2
int y = 0;
by_ref(y); std::cout << y << std::endl; // prints 2
by_val(y); std::cout << y << std::endl; // prints 2
}
Ref ile geçerken temel olarak değişkene bir işaretçi geçirirsiniz. Değer ile geçerken değişkenin bir kopyasını geçirirsiniz. Temel kullanımda bu normalde değişkende ref ile yapılan değişikliklerin çağıran yöntem tarafından görüleceği, değer ile yapılan değişikliklerin ise görülmeyeceği anlamına gelir.