Pass by Value, Reference, Address
Here are two examples. reference[1]
pass by value
void passByValue(int n) {
n = 12;
}
int main() {
int n = 10;
passbyValue(n);
printf("n = %d\n", n); // output 10
}
pass by address
voiod passByAddress(int *p) {
*p = 12;
}
void main() {
int n = 10;
passByAddress(&n);
printf("n = %d\n", n); // output 12
}
pass by reference
Notice
pass by reference only available at C++
Any passed variables, for example n
, will be affected both in inner funtion and outer function.
void passByReference(int & n) {
n = 12;
}
void main() {
int n = 10;
passByReference(n);
printf("n = %d", n); // output 12
}
comparasion
security
method | safety | reason | |
---|---|---|---|
pass by value | đ | address will not be modified. | |
pass by address | đ | address might be modified. | |
pass by reference | đ | address will not be modified. |