U E D R , A S I H C RSS

Hardcore Cpp Study/첫숙제/Value Vs Reference/변준원


가 매개변 달될 때 변 값만
바뀌


매개변되는 변
매개변되는 변 변경
란 무가?

▶ 값 (Call by Value)

C 기본 달 방법.
매개 변 .
로그램 다른 들과 동급.
기법 대로 변 .

( Call by Reference )

달 기법 매개 변 매개 변 보내는 방법.
달 ( )
(call by reference, call by address, call by location) 방법 변경 변경
되는 값 러 개나 배 는 경 .



고 값는 부 로그램로 C++로그램
바로 main() 때 main() 고 또 main() 다른 를 부를
다.
개를 면..


1)
,, 되며 Compiler 리되 object file 만들며 Pro
gramming가 됩다.
2) 매
#define 되는 .
3) inline
만 compiler 리되며 매개변 달 방
다.

달 방법
1) 매개변 방법
달 방법다.
- 값 (Call by value)
매개변
- (Call by pointer reference)
매개변
- (Call by reference)
매개변를 공
2) 부변 방법
로 광보를 .


, , 를 들면,
#include

void main()
{
double power(double base, int exponent); // ()
double base,result;
int exponent;

cout << "밑수 및 지수를 입력하시오\n";
while (cin >> base >> exponent)
{
result = power(base, exponent); //
cout << "밑수("<< base <<")" << "지수("<< exponent <<")" << "결과 :"<< result << endl;
}
}

double power (double x, int y) //
{
double ans="x";
for (int i=1;i < y;i++)
ans*="x";
return ans;
}
  • power(double x, int y) 데...? -
  • 그리고 같게 는것 가급 것 같다. -

double power(double base, int exponent); double
고 power 며 double base, int exponent 매개변다. power()라는
보면 명,반, 매개변 똑같다. 만
다르다면 가 발다. 다른 다면 로 끝난다
는 것다.

다.
란 것 바깥 된 변고 main()
로그램내 가능다. 는데
란 그 다.

를 봅다.
#include

void mytest(); //
int x=1,y=2; //

void main()
{
cout << "x="<< x <<" \n"; cout << "y="<< y <<" \n"; mytest(); // 함수선언
cout << "x="<< x <<" \n"; cout << "y="<< y <<" \n";
}

void mytest() //
{
int x="3;" //
cout << "this x="<< x <<" \n"; cout << "this y="<< y <<" \n";
}


과 같다.
x=1
y=2
this x=3
this y=2
x=1
y=2
mytest 내 x 3 되므로 this x=3 되고 그 값들 모두
기 때문 같게 나는데 C++
다는 것는 공기 때문 나가 다른 모르게 를 바
기 때문다. 그래 그가 나면 들고 나 , 보 기가
때문다.


매개변 달방법 다.
방법(Call by value )
: 매개 변 매개변 는 것매개변
다.
#include <iostream.h>
void main()
{
void test(int, int); //
int a = 10, b = 20;
test(a,b); //
cout << "a=" << a <<" " << "b=" << b << endl;
}

void test(int x, int y) //
{
int temp;
temp = x;
x = y;
y = temp;
}


결과는 a=10 b=20만 내부로 x y 20, 10다.
결국 매개변 는다는 것 다.


(Call by point reference)
: 대 는 방 매개변 된다.

#include <iostream.h>
void main()
{
void test(int*, int*); //
int a = 10, b = 20;
test(&a,&b); // , ab
cout << "a=" << a <<" " << "b=" << b << endl;
}

void test(int *x, int *y) //
{
int temp;
temp = *x;
  • x = *y;
  • y = temp;
}


결과는 a=20 b=10 되고 x는 a 고 y는 b 게 된다.


(Call by reference)
: 매개변는 것 매개변 매개 변
내부 매개변 다.

#include <iostream.h>
void main()
{
void test(int&, int&); //
int a = 10, b = 20;
test(a,b); // , 매개 변
cout << "a=" << a <<" " // 참조형이 실매개 변수로 초기화 << "b=" << b << endl;
}

void test(int &x, int &y) //
{
int temp;
temp = x;
x = y;
y = temp;
}


결과로 a=20 b=20 되는데 결국 내부 매개변 는 것다.
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:22
Processing time 0.0240 sec