변 매변 변 만 복
바뀌
매변 는 변 대를
내 매변 는 변 변
란 무?
▶ (Call by Value)
C 본 방.
는 매 변 를 .
를 는 램 복 른 변들 동 .
는 대 변 .
는 매 변 를 .
를 는 램 복 른 변들 동 .
는 대 변 .
▶ ( Call by Reference )
매 변 를 대는 매 변 보내는 방.
를 ( 는 를 )
(call by reference, call by address, call by location) 방 변면 변
는 러 배 를 는 .
를 ( 는 를 )
(call by reference, call by address, call by location) 방 변면 변
는 러 배 를 는 .
란 료 동 는 부 램 C++램 는
바 main() main() 동 불 main() 른 를 부를
.
먼 대 를 면..
바 main() main() 동 불 main() 른 를 부를
.
먼 대 를 면..
1)
,, 며 Compiler 리 object file 만들며 Pro
gramming 됩.
2) 매
리 #define 는 .
3) inline
매 만 compiler 리며 동 매변 방
.
보 방
1) 매변 방
보 방 .
2) 부변 방
보를 등 .
1) 매변 방
보 방 .
- (Call by value)
매변 를 - (Call by pointer reference)
매변 를 - (Call by reference)
매변를 2) 부변 방
보를 등 .
, , 대 를 들면,
#include
#include
void main()
{
{
double power(double base, int exponent); // ()
double base,result;
int exponent;
cout << "밑수 및 지수를 입력하시오\n";
while (cin >> base >> exponent)
{
} double base,result;
int exponent;
cout << "밑수 및 지수를 입력하시오\n";
while (cin >> base >> exponent)
{
result = power(base, exponent); //
cout << "밑수("<< base <<")" << "지수("<< exponent <<")" << "결과 :"<< result << endl;
} cout << "밑수("<< base <<")" << "지수("<< exponent <<")" << "결과 :"<< result << endl;
double power (double x, int y) //
{
미 power 명 미며 double base, int exponent 매변를 미. power()는
보면 명,, 매변 . 만
르면 러 . 른 면 마막 미
는 .
{
double ans="x";
for (int i=1;i < y;i++)
}for (int i=1;i < y;i++)
ans*="x";
return ans; - power(double x, int y) 따 데...? -동
- 리 는 . -동
미 power 명 미며 double base, int exponent 매변를 미. power()는
보면 명,, 매변 . 만
르면 러 . 른 면 마막 미
는 .
변 변 대 봅.
먼 변란 떤 바 변는 범 main()를
램내 디 능. 변는 몸 는데
변란 만 .
먼 변란 떤 바 변는 범 main()를
램내 디 능. 변는 몸 는데
변란 만 .
를 봅.
#include
#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";
} cout << "x="<< x <<" \n"; cout << "y="<< y <<" \n";
void mytest() //
{
{
int x="3;" // 변
cout << "this x="<< x <<" \n"; cout << "this y="<< y <<" \n";
}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++는 변를 를 는
는 데 는 변는 료 문 느 른 모르 변를 바
문. 면 들 , 보
문.
x=1
y=2
this x=3
this y=2
x=1
y=2
mytest 내 변 x 3 므 this x=3 들 모 변
문 는데 C++는 변를 를 는
는 데 는 변는 료 문 느 른 모르 변를 바
문. 면 들 , 보
문.
매변 방 봅.
방(Call by value )
: 매 변 매변 는 매변
void main()
{
방(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;
}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;
}temp = x;
x = y;
y = temp;
는 a=10 b=20만 내부 x y 20, 10.
매변 변 는는 .
매변 변 는는 .
(Call by point reference)
: 대 는 방 매변 변 .
: 대 는 방 매변 변 .
#include <iostream.h>
void main()
{
void main()
{
void test(int*, int*); //
int a = 10, b = 20;
test(&a,&b); // , ab
cout << "a=" << a <<" " << "b=" << b << endl;
}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;
temp = *x;
- x = *y;
- y = temp;
는 a=20 b=10 x는 a y는 b .
(Call by reference)
: 매변를 는 매변 매 변
void main()
{
: 매변를 는 매변 매 변
내부 매변를 .
#include <iostream.h>void main()
{
void test(int&, int&); //
int a = 10, b = 20;
test(a,b); // , 매 변
cout << "a=" << a <<" " // 참조형이 실매개 변수로 초기화 << "b=" << b << endl;
}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;
}temp = x;
x = y;
y = temp;
a=20 b=20 는데 내부 매변를 는 .