6 include stdio h int mainvoid int num

  • Slides: 64
Download presentation
6장. 함수 #include <stdio. h> int main(void) { int num; printf(“Please enter an integer:

6장. 함수 #include <stdio. h> int main(void) { int num; printf(“Please enter an integer: "); scanf("%d", &num); if ( num < 0 ) printf("Is negative. n"); printf("num = %dn", num); return 0; } 1

함수의 기본 § 함수의 호출 인자와 리턴 값을 갖지 않는 함수의 호출 예(1/2) 01:

함수의 기본 § 함수의 호출 인자와 리턴 값을 갖지 않는 함수의 호출 예(1/2) 01: /* Ex 06_01. c */ 02: #include <stdio. h> 03: 04: void Print. Hello(void) 05: { 06: printf("Hello Worldn"); 07: } 08: 09: void Print. Line(void) 10: { 11: int i; 12: for(i = 0 ; i < 20 ; i++) 13: printf("-"); 14: printf("n"); 15: } 16: Print. Hello 함수의 정의 Print. Line 함수의 정의 6장. 함수 17

함수의 기본 § 함수의 호출 리턴 값은 없고, 두 개의 인자를 갖는 함수의 호출

함수의 기본 § 함수의 호출 리턴 값은 없고, 두 개의 인자를 갖는 함수의 호출 예 01: /* Ex 06_02. c */ 02: #include <stdio. h> 03: 04: void Print. Sum. And. Product(int a, int b) 05: { 06: printf("%d + %d = %dn", a, b, a + b); 07: printf("%d * %d = %dn", a, b, a * b); 08: } 09: 10: int main(void) 11: { 12: int x, y; 13: 14: Print. Sum. And. Product(10, 20); 15: 16: printf("정수를 입력하세요 : "); 17: scanf("%d %d", &x, &y); 18: Print. Sum. And. Product(x, y); 19: 20: return 0; 21: } 함수의 정의 인자를 갖는 함수의 호출 6장. 함수 20

함수의 기본 § 함수의 호출 인자와 리턴 값을 갖는 함수의 호출 예(1/2) 01: /*

함수의 기본 § 함수의 호출 인자와 리턴 값을 갖는 함수의 호출 예(1/2) 01: /* Ex 06_03. c */ 02: #include <stdio. h> 03: 04: int Get. Factorial(int num) 05: { 06: int i; 07: int fact = 1; 08: for(i = 1 ; i <= num ; i++) 09: fact *= i; 10: return fact; 11: } 12: 13: int Get. Sum(int num) 14: { 15: int i; 16: int sum = 0; 17: for(i = 1 ; i <= num ; i++) 18: sum += i; 19: return sum; 20: } 21: Get. Factorial 함수의 정의 Get. Sum 함수의 정의 6장. 함수 22

함수의 기본 § 함수의 호출 인자와 리턴 값을 갖는 함수의 호출 예(2/2) 22: int

함수의 기본 § 함수의 호출 인자와 리턴 값을 갖는 함수의 호출 예(2/2) 22: int main(void) 23: { 24: int result 1, result 2; 25: 26: result 1 = Get. Factorial(10); 27: printf("10 팩토리얼 = %dn", result 1); 28: 29: result 2 = Get. Sum(10); 30: printf("1~10의 합계 = %dn", result 2); 31: 32: return 0; 33: } 인자와 리턴 값을 갖는 함수의 호출 6장. 함수 23

함수의 기본 § 함수의 선언 함수의 정의보다 앞쪽에서 함수를 호출하는 예(1/2) 01: /* Ex

함수의 기본 § 함수의 선언 함수의 정의보다 앞쪽에서 함수를 호출하는 예(1/2) 01: /* Ex 06_05. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i_res; 07: float f_res; 08: 09: i_res = Get. Factorial(5); 10: printf("5! = %dn", i_res); 11: 12: f_res = Get. Max(0. 5, 12. 5); 13: printf("최대값 = %fn", f_res); 14: 15: return 0; 16: } 17: 아직 정의되지 않은 함수의 호출 (컴파일 경고) 6장. 함수 29

함수의 기본 § 함수의 선언 함수의 정의보다 앞쪽에서 함수를 호출하는 예(2/2) 18: int Get.

함수의 기본 § 함수의 선언 함수의 정의보다 앞쪽에서 함수를 호출하는 예(2/2) 18: int Get. Factorial(int num) 19: { 20: int i; 21: int fact = 1; 22: for(i = 1 ; i <= num ; i++) 23: fact *= i; 24: return fact; 25: } 26: 27: float Get. Max(float a, float b, float c) 28: { 29: float max; 30: max = a > b ? a : b; 31: max = c > max ? c : max; 32: return max; 33: } Get. Factorial 함수의 정의 잘못된 실행 결과 Get. Max 함수의 정의 6장. 함수 30

함수의 기본 § 함수의 선언을 사용하는 경우의 예(1/2) 01: /* Ex 06_06. c */

함수의 기본 § 함수의 선언을 사용하는 경우의 예(1/2) 01: /* Ex 06_06. c */ 02: #include <stdio. h> 03: 04: int Get. Factorial(int num); 05: float Get. Max(float a, float b, float c); 06: 07: int main(void) 08: { 09: int i_res; 10: float f_res; 11: 12: i_res = Get. Factorial(5); 13: printf("5! = %dn", i_res); 14: 15: f_res = Get. Max(0. 5, 12. 5); 16: printf("최대값 = %fn", f_res); 17: 18: return 0; 19: } 20: 함수의 선언 함수의 호출 6장. 함수 33

함수의 기본 § 함수의 선언을 사용하는 경우의 예(2/2) 21: int Get. Factorial(int num) 22:

함수의 기본 § 함수의 선언을 사용하는 경우의 예(2/2) 21: int Get. Factorial(int num) 22: { 23: int i; 24: int fact = 1; 25: for(i = 1 ; i <= num ; i++) 26: fact *= i; 27: return fact; 28: } 29: 30: float Get. Max(float a, float b, float c) 31: { 32: float max; 33: max = a > b ? a : b; 34: max = c > max ? c : max; 35: return max; 36: } Get. Factorial 함수의 정의 Get. Max 함수의 정의 6장. 함수 34

지역 변수와 전역 변수 § 지역 변수 다른 함수에 선언된 변수의 사용 01: /*

지역 변수와 전역 변수 § 지역 변수 다른 함수에 선언된 변수의 사용 01: /* Ex 06_07. c */ 02: #include <stdio. h> 03: 04: void Print. Count(void); 05: 06: int main(void) 07: { 08: int count = 0; 09: 10: printf("main: count = %dn", count); 11: 12: return 0; 13: } 14: 15: void Print. Count(void) 16: { 17: printf("Print. Count: count = %dn", count); 18: } 6장. 함수 main 함수 안에서는 count 사용 가능 Print. Count 함수 안에서 count 사용시 컴파일 에러 39

지역 변수와 전역 변수 § 지역 변수 서로 다른 함수에서 같은 이름의 변수를 선언하는

지역 변수와 전역 변수 § 지역 변수 서로 다른 함수에서 같은 이름의 변수를 선언하는 경우 01: /* Ex 06_08. c */ 02: #include <stdio. h> 03: 04: void Print. Count(void); 05: 06: int main(void) 07: { 08: int count = 0; main 함수 안에 지역 변수 count 선언 09: printf("main: count = %dn", count); main 함수 안에 선언된 count 사용 10: 11: Print. Count( ); 12: 13: return 0; 14: } 15: 16: void Print. Count(void) 17: { 18: int count = 100; Print. Count 함수 안에 지역 변수 count 선언 19: printf("Print. Count: count = %dn", count); 20: Print. Count 함수 안에 선언된 count 사용 20: } 6장. 함수 41

지역 변수와 전역 변수 § 지역 변수 같은 함수를 여러 번 호출하는 경우 01:

지역 변수와 전역 변수 § 지역 변수 같은 함수를 여러 번 호출하는 경우 01: /* Ex 06_09. c */ 02: #include <stdio. h> 03: 04: void Test. Local(void); 05: 06: int main(void) 07: { 08: Test. Local( ); 같은 함수를 여러 번 호출 09: Test. Local( ); 10: 11: return 0; 12: } 13: 14: void Test. Local(void) 15: { 16: int num = 0; 지역 변수 num은 함수가 호출될 때마다 새로 생성 17: 18: printf("num = %dn", num++); 19: } 6장. 함수 42

지역 변수와 전역 변수 § 전역 변수의 선언 및 사용(1/2) 01: /* Ex 06_11.

지역 변수와 전역 변수 § 전역 변수의 선언 및 사용(1/2) 01: /* Ex 06_11. c */ 02: #include <stdio. h> 03: 04: void Print. Count(void); 05: void Increment(void); 06: void Decrement(void); 07: 08: int count; 09: 10: int main(void) 11: { 12: count = 0; 13: 14: Print. Count( ); 15: Increment( ); 16: Increment( ); 17: Print. Count( ); 18: 전역 변수 num의 선언 전역 변수 num의 사용 6장. 함수 45

지역 변수와 전역 변수 § 전역 변수 함수 사이에 선언된 전역 변수(2/2) 19: Decrement(

지역 변수와 전역 변수 § 전역 변수 함수 사이에 선언된 전역 변수(2/2) 19: Decrement( ); 20: Print. Count( ); 21: 22: return 0; 23: } 24: 25: void Print. Count(void) 26: { 27: printf("count = %dn", count); 전역 변수 num의 사용 28: } 29: 30: void Increment(void) 31: { 32: count++; 전역 변수 num의 사용 33: } 34: 35: void Decrement(void) 36: { 전역 변수 num의 사용 37: count--; 38: } 6장. 함수 46

지역 변수와 전역 변수 § 전역 변수 함수 사이에 선언된 전역 변수(1/2) 01: /*

지역 변수와 전역 변수 § 전역 변수 함수 사이에 선언된 전역 변수(1/2) 01: /* Ex 06_12. c */ 02: #include <stdio. h> 03: 04: void Print. Count(void); 05: void Increment(void); 06: void Decrement(void); 07: 08: int main(void) 09: { 10: count = 0; 11: 12: Increment( ); 13: Print. Count( ); 14: 15: return 0; 16: } 17: 18: int count; 19: 10번째 줄에서 컴파 일 에러가 발생하므 로 실행할 수 없다! 선언 전에 전역 변수 count를 사용했으므로 컴파일 에러 전역 변수 count의 선언 6장. 함수 48

지역 변수와 전역 변수 § 전역 변수 함수 사이에 선언된 전역 변수(2/2) 20: void

지역 변수와 전역 변수 § 전역 변수 함수 사이에 선언된 전역 변수(2/2) 20: void Print. Count(void) 21: { 전역 변수 num의 사용 22: printf("count = %dn", count); 23: } 24: 25: void Increment(void) 26: { 27: count++; 전역 변수 num의 사용 28: } 29: 30: void Decrement(void) 31: { 전역 변수 num의 사용 32: count--; 33: } 6장. 함수 49

지역 변수와 전역 변수 § 변수의 영역 규칙(1/2) 01: /* Ex 06_13. c */

지역 변수와 전역 변수 § 변수의 영역 규칙(1/2) 01: /* Ex 06_13. c */ 02: #include <stdio. h> 03: 04: void Test(void); 05: int num = 10; 전역 변수 num의 선언 06: 07: int main(void) 08: { 09: int num = 20; 지역 변수 num의 선언 10: 11: while( 1 ) 12: { 지역 변수 num의 선언 13: int num = 30; 14: 15: printf("num = %dn", num++); while 안에 선언된 지역 변수 num 사용 16: 17: if(num > 25) 18: break; 19: } 6장. 함수 52

함수의 인자 전달 방법 §값에 의한 전달 Swap 함수의 예(1/2) 01: /* Ex 06_14.

함수의 인자 전달 방법 §값에 의한 전달 Swap 함수의 예(1/2) 01: /* Ex 06_14. c */ 02: #include <stdio. h> 03: 04: void Swap(int x, int y); Swap 함수의 선언 05: 06: int main(void) 07: { 08: int a = 10; 09: int b = 20; 10: 11: printf("Swap 전의 a = %d, b = %dn", a, b); 12: 13: Swap(a, b); Swap 함수의 호출 14: 15: printf("Swap 후의 a = %d, b = %dn", a, b); 16: 17: return 0; 18: } 19: 6장. 함수 56

함수의 인자 전달 방법 §값에 의한 전달 매개변수로 함수의 처리 결과를 받아오는 경우 01:

함수의 인자 전달 방법 §값에 의한 전달 매개변수로 함수의 처리 결과를 받아오는 경우 01: /* Ex 06_15. c */ 02: #include <stdio. h> 03: 04: void Get. Sum. Product(int a, int b, int sum, int product); 05: 06: int main(void) 07: { 08: int sum = 0, product = 0; 결과를 받아올 변수의 선언 09: 10: Get. Sum. Product(10, 20, sum, product); 함수 호출 11: 12: printf("sum = %d, product = %dn", sum, product); 13: 14: return 0; 15: } 16: 17: void Get. Sum. Product(int a, int b, int sum, int product) 18: { 19: sum = a + b; 매개변수에 결과 저장 20: product = a * b; 21: } 6장. 함수 합과 곱이 구해지 지 않았다! 60