본문 바로가기
IoT 디바이스 활용/C++

C++ - 함수

by cooluk 2020. 10. 11.

함수

  • 함수의 구조

    image-20200907094012433


  • 함수의 호출
    image-20200907094029474

    #include <iostream>
    using namespace std;
    
    int max(int x, int y);  // 함수 원형
    
    int main(int argc, char const *argv[]) {
        int n;
        n = max(2, 3);
        cout << "함수 호출 결과 : " << n << endl;
        return 0;
    }
    
    int max(int x, int y) {
        if(x>y)
            return x;
        else
            return y;
    }

    함수 호출 결과 : 3



  • 함수 원형
    image-20200907094323162
    image-20200907094329858

  • 함수 인자 전달 방법

    • call by value
      image-20200907104010492
```c++
#include <iostream>
using namespace std;
void swap(int x, int y)
{
    int t;
    t = x;
    x = y;
    y = t;
}
int main(int argc, char const *argv[])
{
    int a = 100, b = 200;
    printf("a=%d, b=%d\n", a, b);
    swap(a, b);
    printf("a=%d, b=%d\n", a, b);
    return 0;
}
```

a=100, b=200
a=100, b=200

---


  • call by reference
    image-20200907104226620

    • 참조 변수 - 기존 변수에 새로운 이름을 추가하는 것
      image-20200907104318512

    #include <iostream>
    using namespace std;
    void swap(int &x, int &y)
    {
        int t;
        t = x;
        x = y;
        y = t;
    }
    int main(int argc, char const *argv[])
    {
        int a = 100, b = 200;
        printf("a=%d, b=%d\n", a, b);
        swap(a, b);
        printf("a=%d, b=%d\n", a, b);
        return 0;
    }

    a=100, b=200
    a=200, b=100


    image-20200907104534513

    image-20200907104637550

    image-20200907104701424

    image-20200907104717232

    image-20200907104737657

    image-20200907104751554


  • call by address(pointer)

    • 추후


  • 중복함수 (overload)

    • 함수의 이름은 동일하지만 함수의 인자가 다르면 다른 함수로 인식
    • 리턴 타입은 상관없음

    image-20200907104924308
    image-20200907104940379
    image-20200907104944001

    #include <iostream>
    using namespace std;
    int square(int i)
    {
        cout << "square(int) 호출" << endl;
        return i * i;
    }
    double square(double i)
    {
        cout << "square(double) 호출" << endl;
        return i * i;
    }
    int main(int argc, char const *argv[])
    {
        cout << square(10) << endl;
        cout << square(2.0) << endl;
        return 0;
    }

    square(int) 호출
    100
    square(double) 호출
    4



    • 인수의 디폴트 값

      • 함수 호출시 인수 값을 지정하지 않았을 때 가지는 값
      #include <iostream>
      using namespace std;
      void display(char c = '*', int n = 10)
      {
          for (int i = 0; i < n; i++)
          {
              cout << c;
          }
          cout << endl;
      }
      int main(int argc, char const *argv[])
      {
          display();
          display('#');
          display('#', 5);
          return 0;
      }

      **********

      ##########
      #####



    • 인수의 디폴트 값 지정시 주의 사항

      • 뒤에서 부터 배정
      • 앞에서 부터 배정하는 경우 에러

      image-20200907110356720


    • 예제(함수의 변수를 배열로 받기)

      #include <iostream>
      using namespace std;
      
      // int array[] = {1, 2, 3, ...};
      void initArray(int array[], int size, int value = 0) {
          for(int i=0; i < size; i++) {
              array[i] = value;
          }
      }
      
      // 배열1 = 배열2;  // 값 복사 x --> 배열은 call by value가 안됨.
      
      void printArray(int array[],int size) {
          for(int i=0; i < size; i++){
              cout << array[i] << ", ";
          }
          cout << endl;
      }
      
      int main(int argc, char const *argv[]) {
          int intList[10];
      
          initArray(intList, 10, 100); // 100으로 초기화
          printArray(intList, 10);
      
          initArray(intList, 10);  // 0으로 초기화
          printArray(intList, 10);
      
          return 0;
      }

      100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0,


      배열의 크기를 매개변수로 전달 (sizeof 이용은 추후)


'IoT 디바이스 활용 > C++' 카테고리의 다른 글

C++ - 클래스와 객체  (0) 2020.10.11
C++ - 문자열  (0) 2020.10.11
C++ - 배열  (0) 2020.10.11
C++ - 제어 구조  (0) 2020.10.09
C++ - 기초 사항  (0) 2020.10.09

댓글