12 includestdio h int x POWnint int int

  • Slides: 32
Download presentation

예제 프로그램(1/2) #include<stdio. h> int x. POWn(int, int); -int x. POWn(int, int); 함수의 선

예제 프로그램(1/2) #include<stdio. h> int x. POWn(int, int); -int x. POWn(int, int); 함수의 선 언(declaration) int main(void) -x. POWn(i, j) 함수의 호출(call) { -i, j 함수의 인자(parameter) int i, j; scanf(“%d”, &i); scanf(“%d”, &j); while(i <= 0 || j <= 0){ scanf(“%d”, &i); scanf(“%d”, &j); } printf(“x^n = %dn“, x. POWn(i, j)); return 0; } 3

예제 프로그램(2/2) int x. POWn(int x, int n) -함수의 정의(definition) { -int x, int

예제 프로그램(2/2) int x. POWn(int x, int n) -함수의 정의(definition) { -int x, int n 함수 x. POWn의 인자 int i, retval = 1; for(i = 1; i <= n; i++){ retval *= x; } return retval; } 4

함수의 선언 (Declarations of Functions) n 함수의 선언 n n 컴파일러에게 함수의 이름과 결과값(return

함수의 선언 (Declarations of Functions) n 함수의 선언 n n 컴파일러에게 함수의 이름과 결과값(return value)의 타입, 인자의 타입들을 알려준다. int x. POWn(int, int); n return type 함수이름(type of parameter 1, type of parameter 2, etc. ); 6

함수의 정의 (Definitions of Functions) n 함수의 정의 n n n 함수가 하는 일을

함수의 정의 (Definitions of Functions) n 함수의 정의 n n n 함수가 하는 일을 정의한다. return type 함수이름(parameter 1, parameter 2, etc. ){함수본체} parameter 1: type 1 name 1 7

함수 정의 예제 프로그램 int x. POWn(int x, int n) { int i, retval

함수 정의 예제 프로그램 int x. POWn(int x, int n) { int i, retval = 1; for(i = 1; i <= n; i++){ retval *= x; } -int 함수의 return value type -int x, int n 함수의 인자 (parameter) -return retval; 계산 결과를 return하는 statement return retval; } 9

return 문 예제 프로그램 #include<stdio. h> int absolute_val(int a) { if(a < 0) return

return 문 예제 프로그램 #include<stdio. h> int absolute_val(int a) { if(a < 0) return –a; else return a; -한 함수 내에 return statement는 여러 개 존재 할 수 있다. (참고) unary operator - : -7은 연산 자 –와 정수상수 7로 이루어진 expression이다. } int main(void) { printf(“%d”, absolute_val(-7)); return 0; } 14

Call by Value(1/2) #include<stdio. h> int main(void) int compute_sum(int n) { int n =

Call by Value(1/2) #include<stdio. h> int main(void) int compute_sum(int n) { int n = 3, sum; { int sum = 0; for(; n > 0; --n) sum += n; printf(“%dn”, n); return sum; sum = compute_sum(n); printf(“%dn”, n); } printf(“%dn”, sum); -함수 compute_n()의 인자인 n 과 main 함수에서 사용되는 n은 별개의 객체들이다. return 0; } 16

Call by Value(2/2) 17

Call by Value(2/2) 17

Recursions n Recursive functions (recursive call) n 자기자신을 호출하는 함수 20

Recursions n Recursive functions (recursive call) n 자기자신을 호출하는 함수 20

Recursions 예제 프로그램 #include<stdio. h> int factorial(int n) -int factorial(int) 함수는 recursive functions이다. {

Recursions 예제 프로그램 #include<stdio. h> int factorial(int n) -int factorial(int) 함수는 recursive functions이다. { -빨간 부분이 자신을 호출하는 부분 if(n <= 1) return 1; else return (n * factorial(n – 1)); } int main(void) { printf(“%dn”, factorial(7)); return 0; } 21

Recursive Call 함수 호출 factorial(7) factorial(6) factorial(5) factorial(4) factorial(3) factorial(2) factorial(1) Return value 7

Recursive Call 함수 호출 factorial(7) factorial(6) factorial(5) factorial(4) factorial(3) factorial(2) factorial(1) Return value 7 * factorial(6) 6 * factorial(5) 5 * factorial(4) 4 * factorial(3) 3 * factorial(2) 2 * factorial(1) 1 -예제 프로그램 에서 factorial() 함수는 7번 호 출된다. -최종적으로 factorial(7)은 7*6*5*4*3*2*1 을 return 한다. 22

extern class 예제 프로그램 #include<stdio. h> int a=1, b=2, c=3; /* global variables */

extern class 예제 프로그램 #include<stdio. h> int a=1, b=2, c=3; /* global variables */ int f(void); int main(void) { printf(“%3 dn”, f()); /* 12가 찍힌다. */ printf(“%3 d%3 d%3 dn”, a, b, c); /* 4 2 3이 찍힌다. */ return 0; } int f(void) { int b, c; a = b = c = 4; return (a + b + c); } 28

static external variables n 변수가 선언된 파일에서만 참조가 가능하다. void f(void) { … }

static external variables n 변수가 선언된 파일에서만 참조가 가능하다. void f(void) { … } static int v; void g(void) { … 31

Default Initialization n n extern, static: 모두 0으로 초기화 register, auto: 알 수 없다.

Default Initialization n n extern, static: 모두 0으로 초기화 register, auto: 알 수 없다. 32