26
7 주 주주 Functions

7 주 강의

  • Upload
    vesta

  • View
    53

  • Download
    1

Embed Size (px)

DESCRIPTION

7 주 강의. Functions. Functions. Top-down programming type function_name ( parameter_list ) { declarations statements } int factorial(int n) { int i, product = 1; for (i=2; i

Citation preview

Page 1: 7 주 강의

7 주 강의

Functions

Page 2: 7 주 강의

Functions

• Top-down programming• type function_name (parameter_list) { declarations statements }• int factorial(int n) { int i, product = 1; for (i=2; i<= n; ++I) product *=i; return product; }• fact = facorial(5) ;

Page 3: 7 주 강의

예• void nothing(void) { } /* do nothing */• double twice(double x) { return (2.0 * x) ; }• int all_add(int a, int b) { int c; …. return(a+b+c) }

Page 4: 7 주 강의

Functions in traditional C

• void f(a, b, c, x, y) int a, b, c; double x, y { …. }

Page 5: 7 주 강의

The return Statement

• return_statement ::= return ; | return expression ;• return; return ++a; return (a * b);• double_absolute_value (double x) { if (x>=0.0) return x; else return –x; }

Page 6: 7 주 강의

Function Prototypes

• Functions should be declared before they are used

• double sqrt(double)• type function_name(parameter type list) ; • Identifier is optional (void f(char c, int i); voi

d f(char, int)

• double sqrt(); /* traditional C style */ • 203 page 예제 설명

Page 7: 7 주 강의

Function declarations from the compiler’s viewpoint

• function invocation, function definition, function declaration, function prototypes

• Function storage class 는 extern 이나 static 이어야지 auto 나 register 는 안 된다

• function definition 이 있으면 function prototype 은 없어도 되지만 쓰는 게 좋다 .

• 아무 선언 없이 f(x) 를 쓰면 ‘ int f();’ 로 보거나 오류• int f(x) /* f(1) 은 오류가 될 수 있다 */ double x;• int f(double x) /* 이 때는 int 가 double 로 바뀜 */

Page 8: 7 주 강의

An alternate style

#include <stdio.h> #define N 7 void prn_heading(void) { …} long power(int m, int n) {…} … int main(void) {prn_heading(); …}

Page 9: 7 주 강의

Function invocation and call-by-value

• main() 에서 수행을 시작• Function 이름을 만나면 invoked(called) 된다• Function 의 argument 수와 type 이 일치해야• call-by-value 를 사용

Page 10: 7 주 강의

Function invocation order

1. Each expression in argument list(actual parameters) is evaluated

2. 그 결과가 formal parameter( 형식인자 ) 의 형과 다르면 변환한다

3. body 를 수행한다4. return 문을 수행한다 5. return 문이 expression 을 가졌으면 수행하고 ,

형이 맞지 않으면 변환한다6. return 에 아무 expression 이 없으면 아무 결과도 안

돌려준다

Page 11: 7 주 강의

Developing a large program

• 큰 프로그램이 다수 파일에 있을 때는 “ make” 를 사용

• #include “pgm.h” 현재 주소 검사 , 시스템에 제공하는 곳 조사

• 210, 211 page 를 설명

Page 12: 7 주 강의

Using assertions

• #include <assert.h> #include <stdio.h> ….. c=f(a,b); assert(c>0); ….• NDEBUG

Page 13: 7 주 강의

Scope rules

• 변수의 범위규칙• { int a,b; …. {float b; …. } {float a; …. } }

Page 14: 7 주 강의

Using a block for debugging

• { static int cnt = 0; print(“*** debug: cnt =%d v = %d\n”, ++cnt, v);

• Local 변수를 이용한 오류 찾기

Page 15: 7 주 강의

Storage Classes

• auto, extern, register, static• auto ::: by default• automatic 의 준말 • auto int a, b, c; auto float f; • ㅁ uto 는 block 에 들어갈 때마다

기억장소를 배정 , 초기화되지 않음

Page 16: 7 주 강의

extern

• extern ::: variables declared outside a function

• memory 를 계속 유지 , 초기화함 (0 으로 ), 전역변수 (global variable)• 예 ::: 217page• extern int a=1, b=2; /*extern 은 없어도

됨 ; 전통 C 는 안 된다 */ int main(void) …

Page 17: 7 주 강의

extern for separate compiling

• int a=1, b=2, c=3; int f(void) int main(void) { …} int f(void) { extern int a; …}• Side effect 가 일어남• extern double sin(double) /* function prototype */• extern double sin(double x) { … }

Page 18: 7 주 강의

The storage class register

• { register int i; for (i=0; I<LIMIT; ++i) { …} }• register i; register int i • 값이 초기화되지 않음

Page 19: 7 주 강의

The storage class static

• Void f(void) { static int cnt = 0; ++cnt; if (cnt % 2 ==0) ….. else ….. }

Page 20: 7 주 강의

Static external variable

• void f(void) { … /* v is not available here */ } static int v; /* static external variable */ void g(void) { … /* v can be used here */ }• 221, 222 page 프로그램 설명

Page 21: 7 주 강의

Default initialization

• automatic, register ::: 초기화 안 됨

• external, static ::: 초기화됨

Page 22: 7 주 강의

Recursion

• int sum(int n) { if (n <= 1) return n; else return (n + sum(n-1)); }

Page 23: 7 주 강의

Factorial

• int factorial(int n) { if (n <= 1) return 1; else return (n*factorial(n-1)); }

Page 24: 7 주 강의

Recursion and efficiency

• recursion 은 수행속도가 느리다 . 그러나 개념적으로 보면 명확한 표현이 가능하다 . 그리고 모든 recursion 은 stack 을 이용하면 iteration 으로 바꿀 수 있다…

• Factorial 을 iteration 으로 바꿀 수 있다• 227page 설명

Page 25: 7 주 강의

Towers of Hanoi

• 설명

Page 26: 7 주 강의

숙제• 학교에서 ::: 1, 3, 5, 15, 17, 22• 집 ::: 7, 8, 11, 14, 16, 23, 26