421페이지
parameter 전달 방식에 따른 5가지 모델
1. pass-by-value
actual parameter가 formal parameter의 초기화 값으로 쓰임.
actual parameter의 값만 copy됨.
이후부터는 formal parameter는 local 변수로 쓰임.
장점은 linkage cost, access time에서 빠르다.
단점은 extra storage (formal parameter를 저장하기 위한 공간이 또 필요하다는 것)
parameter가 어레이 이거나 큰 것일 때 copy 수행에 대한 부
2. pass by result
결과값을 actual parameter에 copy
단점: extra storage (위와 마찬가지로),
collision 가능성도 있음
----------------
sub(a,a);
sub(int &b, int &c)
{
b++;
c--;
}
?????
------------------
3. pass by value(in out mode)
단점
받아올때도 copy하고
돌려줄때도 copy하니까
storage와 time이 두배로 든다
collision문제도 있다.
4. pass by reference
access path(address)를 전송
장점: 시간과 공간이 efficient
단점:
다소 slow(indirect access이므로)
단뱡향 전달만을 원할 때 구현이 어려워짐
alias효과에 의한 부작용 가능성
5. pass by name
formal parameter가 call 시점에 값이나 주소값이 아닌 method에 bound되고, 실제 value나 address에 대 binding은 formal parameter가 사용될 때 비로소 이루어진다.
예시
int i=1;
sub(arr[i]);
-----
sub(myParam)
{
myParam = 3;
i =i+1;
myParam =5;
}
이렇게 하면
arr[1] = 3;
arr[2] = 5;
이렇게 들어간다.
형식을 자동으로 파악하는 신기한 기능!
장점:
late binding효과 (사용될때까지 늦춘다) ->flexibility
actual parameter
단점:
속도 저하
언어 구현이 어렵다 (difficult to implement)
read, write가 어렵다