티스토리 뷰

LANGUAGE/C++

[C++] CALL BY

진심스테이크 2018. 3. 5. 15:06

 

call by value : 값을 사용

call by adress : 주소값을 지정해서 사용

call by reference : '*'를 쓰지않고 포인터 처럼 값을 넣어서 사용

 

#include "stdafx.h"
#include <iostream>
using namespace std;

//사용자 정의 함수 선언
void disp(int a);
int input(int a);
void input_add(int *a);
void input_ref(int &a);

void main() {
    int a = 10;

    disp(a);
    a = input(a); //call by value
    input_add(&a); //call by address
    input_ref(a); //call by reference
    disp(a);

    //int &a //int메모리를 a를 참조하여 다시 부름
}

void input_ref(int &a) {
    cout << "input : ";
    cin >> a; //main의 a
}

void input_add(int *a) {
    cout << "input : ";
    cin >> *a;
}

void disp(int a) {
    cout << a << endl;
}

int input(int a) {
    cout << "input : ";
    cin >> a;
    return a; //본인을 호출한 곳으로 돌아감 -> main에서의 a 함수
}

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

[C++] DYNAMIC MEMORY - 동적 메모리  (0) 2018.03.06
[C++] POINTER - 포인터  (0) 2018.03.05
[C++] REFERENCE  (0) 2018.03.05
[C++] ARRAY - 배열  (0) 2018.03.05
[C++] BASIC THINGS  (0) 2018.03.05
댓글