C++

C++ 매개변수 전달 총정리

김구티2 2024. 3. 30. 14:41

1. 매개변수 전달의 개념

C++에서는 연산을 수행하기 위해 호출될 때 함수에 데이터를 보내야 한다. 이 데이터를 매개변수 또는 인수라고 하며, C++에서는 각각 장단점이 있는 다양한 매개변수 전달 방법이 있다. 그 다양한 방법에 대해 학습하는 시간을 갖는다.

 

C++에 있는 함수에 매개변수를 전달하는 방법으로는 3가지가 존재한다.

① Value에 의한 전달

② Reference에 의한 전달

③ Pointer에 의한 전달

2. Pass by Value

Pass by Value 방식에서는 변수의 실제 값을 복사한 다음 원래 변수 대신 함수에 전달한다. 따라서 함수 내부의 매개 변수를 변경해도 함수 외부의 변수의 원래 값에는 영향을 미치지 않는다. 이 방식은 상대적으로 이해하고 구현하기 쉽지만, 값을 복사하는 과정을 포함하기 때문에 큰 규모의 데이터 구조에는 그다지 유용하지 않다.

 

예시:

// C++ program to illustrate the pass by value 
#include <iostream> 
using namespace std; 

// function to swap variables 
void swap(int a, int b) 

     int temp = a; 
     a = b; 
     b = temp; 

int main() 

     int x = 5; 
     int y = 10; 

     cout << "Before Swapping:\n"
     cout << "x = " << x << ", y = " << y << endl; 

     // Call the 'swap' function with pass-by-value 
     // parameters 
     swap(x, y); 

     // Print the values of 'x' and 'y' after the 
     // (ineffective) swap The values remain unchanged 
     // because the function works on copies. 
     cout << "After Swapping:\n"
     cout << "x = " << x << ", y = " << y << endl; 

     return 0; 
}

 

출력:

Before Swapping:
x = 5, y = 10
After Swapping:
x = 5, y = 10

3. Pass by Reference

Pass by Reference 방식에서는 인수 자체를 전달하는 대신 인수의 참조를 함수에 전달한다. 이를 통해 함수가 원래 인수의 값을 변경할 수 있다. 함수 내에서 우리의 인수에 변경 사항이 발생하면 우리의 원래 인수에 반영되므로 이 방법으로 데이터를 처리할 때 주의해야 한다.

 

예시:

// C++ program to illustrate the use of pass by reference 
#include <iostream> 
using namespace std; 

// function to swap variables 
void swap(int& a, int& b) 

     int temp = a; 
     a = b; 
     b = temp; 


// driver code 
int main() 

     int x = 5; 
     int y = 10; 
     cout << "Before swap:\n"
     cout << "x = " << x << ", "
     cout << "y = " << y << endl; 

     // Call the 'swap' function with pass-by-reference 
     // parameters Values of 'x' and 'y' are modified in the 
     // calling code because references are used. 
     swap(x, y); 

     cout << "After swap:\n"
     cout << "x = " << x << ", "
     cout << "y = " << y << endl; 
     return 0; 
}

 

출력:

Before swap:
x = 5, y = 10
After swap:
x = 10, y = 5

3. Pass by Pointer

Pass by Pointer는 Pass by Reference 방법과 매우 유사하다고 할 수 있다. 유일한 차이점은 우리가 함수를 참조하는 대신에 원시 포인터를 전달한다는 것이다. 그것은 우리가 인수의 주소를 함수에 전달한다는 것을 의미한다.

 

예시:

// C++ program to illustrate the pass by pointer method. 
#include <iostream> 
using namespace std; 

// function to swap variables 
void swap(int* a, int* b) 

     int temp = *a; 
     *a = *b; 
     *b = temp; 


// driver code 
int main() 


     int x = 5; 
     int y = 10; 

     // Print the values of 'x' and 'y' before the swap 
     cout << "Before swap:\n"
     cout << "x = " << x << " ,"
     cout << "y = " << y << endl; 

     // Call the 'swap' function with pass-by-pointer 
     // parameters 
     // Values of 'x' and 'y' are modified in the calling 
     // code because pointers are used. 
     swap(&x, &y); 

     // Print the values of 'x' and 'y' after the swap 
     cout << "After swap:\n"
     cout << "x = " << x << " ,"
     cout << "y = " << y << endl; 

     return 0; 
}

 

출력:

Before swap:
x = 5 ,y = 10
After swap:
x = 10 ,y = 5

 

728x90

'C++' 카테고리의 다른 글

C++ 디폴트 인수(Default Arguments) 총정리  (0) 2024.04.02
C++ Call by Value와 Call by Reference의 차이  (0) 2024.04.01
C++ return문 총정리  (0) 2024.03.17
C++ 함수(functions) 총정리  (0) 2024.03.16
C++ 범위 기반 for문 총정리  (0) 2024.03.14