Argument == Acutal Parameter 아래에서 3 과 5가 argument 이고, a와 b 가 parameter 이다.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
add(3, 5);
return 0;
}
a 가 포인터는 아니지만, 실제 a 값을 가리키고 있지만,
a 값을 변경하면 (a parameter) 파라미터 임에도 불구하고,
원본이 바뀐다
#include <iostream>
using namespace std;
void call_by_reference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
void call_by_address(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void call_by_value(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int a_1 = 3;
int b_1 = 7;
int a_2 = 3;
int b_2 = 7;
int a_3 = 3;
int b_3 = 7;
call_by_reference(a_1, b_1);
call_by_address(&a_2, &b_2);
call_by_value(a_3, b_3);
printf("%d %d\\n", a_1, b_1);
printf("%d %d\\n", a_2, b_2);
printf("%d %d\\n", a_3, b_3);
return 0;
}
7 3
7 3
3 7
자바에서 pass by value 만을 허용하고, call by reference 를 허용하지 않겠다는 소리는, function call 로 인한 side effect 를 허용하지 않겠다는 언어 설계 철학을 이야기한 것이다.
그냥 swap 이 불가능하면 전부 call by value 라고 생각을 하자.