Presentation is loading. Please wait.

Presentation is loading. Please wait.

6주차: Functions in C and Others

Similar presentations


Presentation on theme: "6주차: Functions in C and Others"— Presentation transcript:

1 6주차: Functions in C and Others

2 함수의 수행 과정 main 함수 ... 함수 A를 호출 함수 A

3 함수 정의의 예 -int: 함수의 return value type -int x, int n: 함수의 인자(parameter)
int xPOWn(int x, int n) { int i, retval = 1; for(i = 1; i <= n; i++){ retval *= x; } return retval; -int: 함수의 return value type -int x, int n: 함수의 인자(parameter) xn을 계산하는 함수 return retval;: 계산 결과를 return하는 statement

4 함수 호출의 예 -앞장의 함수 정의 부분과 이 장의 프로그램은 모두 한 파일에 들어 있다.
#include<stdio.h> int xPOWn(int, int); int main(void) { printf(“x^n = %d\n“, xPOWn(3, 7)); return 0; } -앞장의 함수 정의 부분과 이 장의 프로그램은 모두 한 파일에 들어 있다. -xPOWn(3, 7): x = 3, n = 7로 함수를 호출

5 The void -어떤 값을 return하지 않는 함수는 void type이다.
#include<stdio.h> void print_hello(void) { printf(“Hello World!\n”); } int main(void) print_hello(); return 0; -어떤 값을 return하지 않는 함수는 void type이다. -인자를 받지 않는 함수를 정의할 때에는 인자가 들어갈 자리에 void를 쓴다. -함수의 정의는 함수의 선언의 역할도 동시에 한다.

6 함수의 수행 종료 함수는 두 가지 경우에 그 수행을 종료하고 자신을 호출했던 부분으로 되돌아간다.
return statement를 만났을 때 함수를 다 수행했을 때 함수의 정의대로 다 수행하고 }를 만났을 때

7 The return Statement return; return expression; void 함수인 경우

8 return Statement의 예 -return statement는 여러 개 있을 수도 있다.
#include<stdio.h> int absolute_val(int a) { if(a < 0) return –a; else return a; } int main(void) printf(“%d”, absolute_val(-7)); return 0; -return statement는 여러 개 있을 수도 있다. -unary operator -: -7은 연산자 –와 정수상수 7로 이루어진 expression이다.

9 #include<stdio.h>
파일이름 #include<stdio.h> stdio.h를 프로그램에 삽입한다. stdio.h에는 printf(), scanf 함수를 비롯한 여러 기본 함수들의 선언이 되어 있다.

10 int main(void) main은 C 프로그램의 본체가 되는 함수이다.
프로그램은 항상 main 함수에서 시작해서 main 함수에서 끝난다.

11 C에서의 인자의 전달 함수 호출의 예 각 인자에 해당하는 수식의 값이 계산되어서 함수의 인자로 전달된다. xPOWn(3, 7)
3, 7: 수식 각 인자에 해당하는 수식의 값이 계산되어서 함수의 인자로 전달된다. call-by-value

12 Call-by-Value(1/2) - 인자로 정수 n을 받아서 1부터 n까지의 합을 구하는 함수
#include<stdio.h> int compute_sum(int n) { int sum = 0; for(; n > 0; --n) sum += n; return sum; } – 뒷 장과 연결됨. - 인자로 정수 n을 받아서 1부터 n까지의 합을 구하는 함수

13 Call-by-Value(2/2) int main(void) { int n = 3, sum; printf(“%d\n”, n); sum = compute_sum(n); printf(“%d\n”, sum); return 0; } -함수 compute_n()의 인자인 n과 main 함수에서 사용되는 n은 별개의 객체들이다. -이 프로그램의 결과는 3 6 이다.

14 Comments 주석문 프로그램에 설명을 달 때 사용된다. /* */로 둘러싸인 부분은 프로그램에 전혀 영향을 주지 않는다.

15 The Scope Rule(1/2) block: {}로 싸인 부분을 block이라고 한다. 함수의 정의도 block이다.
#include<stdio.h> void main(void) { int a = 2; printf(“%d\n”, a); /* 2가 찍힌다. */ int a = 5; printf(“%d\n”, a); /* 5가 찍힌다. */ } printf(“%d\n”, ++a); /* 3이 찍힌다. */ block: {}로 싸인 부분을 block이라고 한다. 함수의 정의도 block이다. 변수의 선언은 block의 앞부분에 있어야 한다.

16 The Scope Rule(2/2) -포함된 block은 자신을 포함한 block의 변수들을 접근할 수 있다.
#include<stdio.h> void main(void) { int a = 2; printf(“%d\n”, a); /* 2가 찍힌다. */ printf(“%d\n”, a); /* 2가 찍힌다. */ } printf(“%d\n”, ++a); /* 3이 찍힌다. */ -포함된 block은 자신을 포함한 block의 변수들을 접근할 수 있다.

17 Storage Classes C의 모든 변수와 함수는 type과 storage class를 가진다.
저장되는 장소 주기억장치(RAM), 레지스터(register) auto, extern, register, static

18 C 프로그램의 구성 외부 영역(global) 함수 A main함수 함수 B 함수 K 함수L

19 The auto Class block 내부에 선언된 변수
함수 내부에 선언된 변수 Local variables block이 수행될 때 메모리에 영역이 잡히고 block의 수행이 끝나면 없어진다. 자신이 선언된 block에서만 존재한다.

20 auto Class Example -내부 block의 a는 내부 block의 수행이 끝나면서 없어진다. auto
#include<stdio.h> void main(void) { int a = 2; printf(“%d\n”, a); /* 2가 찍힌다. */ int a = 5; printf(“%d\n”, a); /* 5가 찍힌다. */ } printf(“%d\n”, ++a); /* 3이 찍힌다. */ -내부 block의 a는 내부 block의 수행이 끝나면서 없어진다. auto

21 The extern Storage Class
block 외부에 선언된 변수는 extern 변수이다. 함수는 모두 extern storage class 프로그램이 수행되기 시작할 때 메모리에 영역이 잡히고 프로그램이 종료할 때 없어진다. 프로그램의 모든 영역에서 접근할 수 있다.

22 Extern Class Example (1)
#include<stdio.h> int a=1, b=2, c=3; /* extern variables */ int f(void); int main(void) { printf(“%3d\n”, f()); /* 12가 찍힌다. */ printf(“%3d%3d%3d\n”, a, b, c); /* 이 찍힌다. */ return 0; } int f(void) { int b, c; a = b = c = 4; return (a + b + c); }

23 Extern Class Example (2)
#include<stdio.h> int a=1, b=2, c=3; /* external variables */ int f(void); int main(void) { printf(“%3d\n”, f()); /* 12가 찍힌다. */ printf(“%3d%3d%3d\n”, a, b, c); /* 이 찍힌다. */ return 0; File1.c int f(void) { int b, c; a = b = c = 4; return (a + b + c); } File2.c extern int a;

24 Static Storage Class (1)
void f(void) { static int count = 0; ++count; if(count % 2 ==0) …. else ….. } -Block 에 재진입시에 변수값이 유지되도록 해준다. -함수 f()에서만 접근 가능

25 Static Storage Class (2)
#include<stdio.h> static int a=1, b=2, c=3; /* static external variables */ int f(void); int main(void) { printf(“%3d\n”, f()); /* 12가 찍힌다. */ printf(“%3d%3d%3d\n”, a, b, c); /* 이 찍힌다. */ return 0; File1.c int f(void) { int b, c; a = b = c = 4; return (a + b + c); } File2.c extern int a; error

26 교재에서 강의와 연관된 부분 5장 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.10, 5.11


Download ppt "6주차: Functions in C and Others"

Similar presentations


Ads by Google