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.0267 sec