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