2.1값에 의한 전달


    우선 값에 의한 전달(call by value)부터 알아봅시다. 코드 5-6은 함수 안에서 값을 변경하는 C++ 코드입니다.


    코드 5-6 function/call_by_value.cpp

    #include <iostream>
    using namespace std;
    
    void change_value(int x, int value) // #1
    {
        x = value;                      // #2
        cout << "x : " << x << " in change_value" << endl;
    }
    
    int main(void)
    {
        int x = 10;                     // #3
        change_value(x, 20);            // #4
        cout << "x : " << x << " in main" << endl;
    
        return 0;
    }
    

    실행결과 x : 20 in change_value
    x : 10 in main

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.