7 주 강의

Preview:

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

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<= n; ++I) product *=i; return product; }• fact = facorial(5) ;

예• 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) }

Functions in traditional C

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

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; }

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 예제 설명

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 로 바뀜 */

An alternate style

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

Function invocation and call-by-value

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

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 이 없으면 아무 결과도 안

돌려준다

Developing a large program

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

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

• 210, 211 page 를 설명

Using assertions

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

Scope rules

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

Using a block for debugging

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

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

Storage Classes

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

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

extern

• extern ::: variables declared outside a function

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

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

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) { … }

The storage class register

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

The storage class static

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

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 프로그램 설명

Default initialization

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

• external, static ::: 초기화됨

Recursion

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

Factorial

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

Recursion and efficiency

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

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

Towers of Hanoi

• 설명

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

Recommended