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