ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C Language] 25. 함수 - 리턴 값 여러 개 받기 - C 언어
    CSE/C Language 2015. 6. 13. 10:22

    1. 전역변수를 이용한 리턴 값 받기 

     가장 간단하지만 가장 위험한 전역변수를 이용한 리턴 값 받기를 살펴보자.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    /*
     * global.c
     *
     *  Created on: 2015. 5. 17.
     *      Author: root
     */
     
    #include <stdio.h>
     
    int add, subtract, multiply, divide;
     
    void calculate(intint);
     
    int main(void) {
     
        int x = 7, y = 5;
     
        calculate(x, y);
     
        printf("%d + %d = %d\n", x, y, add);
        printf("%d - %d = %d\n", x, y, subtract);
        printf("%d * %d = %d\n", x, y, multiply);
        printf("%d / %d = %d\n", x, y, divide);
     
     
        return 0;
    }
     
     
    void calculate(int x, int y) {
        add = x + y;
        subtract = x - y;
        multiply = x * y;
        divide = x / y;
    }
    cs








    위 프로그램은 간단해 보여서 좋아보이지만, 최악의 프로그램이다. 이런 프로그래밍은 자제하자.



    2. 포인터를 이용한 리턴 값 받기

     포인터를 이용한 방법은 전역변수에 비해 까다롭지만 가장 많이 이용되는 방식이다.


     아래 예제를 통해 살펴보자.


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    /*
     * pointerreturn.c
     *
     *  Created on: 2015. 5. 17.
     *      Author: root
     */
     
    #include <stdio.h>
     
    void calculate(intintint *int *int *int *);
     
    int main(void) {
     
        int x = 7, y = 5;
        int add, sub, mul, div;
     
        calculate(x, y, &add, &sub, &mul, &div);
     
        printf("%d + %d = %d\n", x, y, add);
        printf("%d - %d = %d\n", x, y, sub);
        printf("%d * %d = %d\n", x, y, mul);
        printf("%d / %d = %d\n", x, y, div);
     
     
        return 0;
    }
     
    void calculate(int x, int y, int *add, int *sub, int *mul, int *div) {
     
        *add = x + y;
        *sub = x - y;
        *mul = x * y;
        *div = x / y;
     
     
    }
    cs









    3. 구조체를 이용한 리턴 값 받기

     구조체를 이용한 방법은 기본적으로 복사가 이루어지기 때문에 많이 사용되지는 않지만 여러모로 유용하므로 알아두자.


     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    /*
     * structReturn.c
     *
     *  Created on: 2015. 5. 17.
     *      Author: root
     */
     
    #include <stdio.h>
     
    struct calc_t {
        int add;
        int sub;
        int mul;
        int div;
    };
     
     
    struct calc_t calculate (intint);
     
     
    int main(void) {
     
        struct calc_t value;
     
        int x = 7, y = 5;
     
        value = calculate(x, y);
     
        printf("%d + %d = %d\n", x, y, value.add);
        printf("%d - %d = %d\n", x, y, value.sub);
        printf("%d * %d = %d\n", x, y, value.mul);
        printf("%d / %d = %d\n", x, y, value.div);
     
     
        return 0;
    }
     
    struct calc_t calculate (int x, int y) {
        struct calc_t tmp;
     
        tmp.add = x + y;
        tmp.sub = x - y;
        tmp.mul = x * y;
        tmp.div = x / y;
     
        return tmp;
    }
    cs










    4. 구조체 포인터를 이용한 리턴 값 받기



     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    /*
     * structPointer.c
     *
     *  Created on: 2015. 5. 17.
     *      Author: root
     */
     
    #include <stdio.h>
     
    struct calc_t {
        int add;
        int sub;
        int mul;
        int div;
    };
     
    void calculate(intintstruct calc_t *);
     
    int main(void) {
     
        int x = 7, y = 5;
        struct calc_t tmp;
     
        calculate(x, y, &tmp);
     
        printf("%d + %d = %d\n", x, y, tmp.add);
        printf("%d - %d = %d\n", x, y, tmp.sub);
        printf("%d * %d = %d\n", x, y, tmp.mul);
        printf("%d / %d = %d\n", x, y, tmp.div);
     
        return 0;
    }
     
    void calculate(int x, int y, struct calc_t * tmp) {
        tmp->add = x + y;
        tmp->sub = x - y;
        tmp->mul = x * y;
        tmp->div = x / y;
    }
    cs









    * Programming in C 서적을 참고하여 작성하였습니다 

    댓글

Designed by Tistory.