컴퓨터 과학/C Language

Chapter 01. 함수(Function)

계란💕 2022. 1. 30. 10:14

 

1.1 지역변수(Local variable)

  - 함수 안에 정의 되어 그 함수 안에서만 참조 가능.

  - 프로그래밍 하면서 일반적으로 선언되는 변수.

  Ex) 지역변수

<hide/>

#include <stdio.h>

int main()
{
    int a = 0 ;
    {
        int a = 10;

        printf("%d\n", a);
    }
    printf("%d\n", a);

    return 0;
}

 

 

1.2 지역변수 초기화

  - 지역 변수의 초기화는 랜덤값.

<hide/>

#include <stdio.h>

int main()
{
    int a;

    printf("%d\n", a );

    return 0;
}

 

1.3 const 변수(Const Variables)

  - 변수를 상수화 할 때 사용.

  - 함수 안에서만 정의되고 그 함수에서만 사용할 수 있음.

<hide/>

#include <stdio.h>

int main()
{
    const int a = 0;

    a = 10;

    printf("%d\n", a);

    return 0;
}

 

1.4 정적 변수(Static Variables)

  - 함수가 실행되면 메모리 영역의 일부를 항상 점유.

  - 함수가 빈번하게 호출되고 종료되어도 특정 값을 항상 유지할 때, 사용되는 문법.

<hide/>

#include <stdio.h>

void func()
{
    static int a = 0;

    printf("%d" , a++);

}

int main()
{
    func();

    func();

    return 0;
}

 

1.5 외부(전역) 변수 (External Variables)

  - 함수 밖에서 정의되고 어느 함수에서도 참조할 수 있음.

  - 초기화는 0 또는 NULL.

  - 함수가 종료되어도 소멸되지 않고 그 값을 계속 유지.

<hide/>

#include <stdio.h>

int a; // 정수 a는 전역 변수

void func()
{
    printf("%d", a++);

}
int main()
{

    func();

    printf("%d\n", a);

    return 0;
}

 

1.6 함수의 정의

  - 함수는 인수를 전달 받아 작업을 수행한 후 결과를 전달하는 프로그램.

 

1.7 함수의 구성

  - 반환형: 호출된 함수에서 호출한 함수로 값을 되돌려 줄 때 "return"문을 사용하여 값을 되돌려줌.

  - : 함수끼리 서로 주고 받는 data. ( 여러 개 지정 가능, 지정하지 않아도 된다)

<hide/>

#include <stdio.h>

int add(int x, int y) // 반환자료형 함수명 (매개자료형 매개변수명)
{
    return x + y; // 반환값
}

int main(void)
{
    int a, b;  //자료형 변수명;

    scanf("%d %d", &a, &b );

    printf("%d", add(a , b));

    return 0;
}

 

1.8 함수의 원형(Prototyoe)

  Ex) int func(int x, int y);

  - 함수의 원형을 선언할 때 변수명은 생략 가능

 

1.9  함수 호출(Call)

<hide/>

int main(); // 전방 선언

#include <stdio.h>

void test()
{
    printf("hello\n");
} // test함수가 종료되면 main함수로 복귀.


{
    test(); // 함수 호출

    return 0;
}

 

1.10 매개변수(인자), 반환값이 있는 함수 형태

<hide/>

#include <stdio.h>

int add(int a, int b); //전방선언, 함수의 원형(prototype)

int main()
{
    int x, y, sum;

    x = 2, y = 3;

    sum = add(x, y);

    printf("%d", sum );

}

int add(int a, int b) // 반환자료형 함수명 (매개자료형 매개변수명);
{

    return a + b; // 반환값;
}

 

1.11 Call by value(값에 의한 전달)

  - "call by value"란 main 함수의 x값을 복사하여 add함수의 x에 복사하기 때문에 "값에 의한 전달"이라고 한다. 

<hide/>

#include <stdio.h>

void add(int x)
{
    x = 100;

    printf("%d\n", x);

}

int main()
{
    int x;

    x = 2;

    printf("%d\n", x);

    add(x);

    printf("%d\n", x);

}

 

 

Note(1)

<hide/>

#include <stdio.h>

// 반환자료형 함수명 (매개자료형 매개변수명)
//{
//    return 반환값;
// }

int main(void)
{
    함수명(); // 함수 호출. function call 
    
    return 0;
}

 

Note(2)

<hide/>

#include <stdio.h>

void print_one_star(void)
{
    printf("*");

    return;
}

int main(void)
{
    print_one_star();

    return 0;
}

 

Note(3)

<hide/>


#include <stdio.h>

void print_sum(int a, int b)
{
    printf("%d", a + b );

    return;
}

int main(void)
{
    print_sum(2, 5); // 인자값(Argument): 2, 5;

    return 0;
}

Note(4)

 

<hide/>

#include <stdio.h>

int add(int a, int b) // 매개변수(Paramerter): a, b;
{
    return a + b;
}

int main(void)
{
    int res = add(2, 5); // 인자값(Argument):2, 5;

    printf("%d", res);

    return 0;
}

'컴퓨터 과학 > C Language' 카테고리의 다른 글

Chapter 03. 구조체 정의  (0) 2022.02.01
Chapter 02. 포인터(Pointer)  (0) 2022.01.30
2022-01-27 재귀함수  (0) 2022.01.28
2022-01-26 구조체 할당  (0) 2022.01.27
2022-01-24 포인터 연습  (0) 2022.01.25