Upcoming SlideShare
×

6. function

259 views
154 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
259
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

6. function

1. 1. Functions
2. 2. Function Overview  Read 3 numbers and print followings – max(first, second ) – max(first, third) – max(second, third) 2 #include <stdio.h> int main() { int a, b, c, m ; scanf( “%d%d%d”, &a, &b, &c ) ; if( a > b ) m = a ; else m = b ; printf( “%dn”, m ) ; if( a > c ) m = a ; else m = c ; printf( “%dn”, m ) ; if( b > c ) m = b ; else m = c ; printf( “%dn”, m ) ; return 0; }
3. 3. Function Overview  Yes, you can do! 3 #include <stdio.h> int main(void) { int a, b, c, m ; scanf( “%d%d%d”, &a, &b, &c ) ; m = max( a, b ) ; printf( “%dn”, m ) ; m = max( a, c ) ; printf( “%dn”, m ) ; m = max( b, c ) ; printf( “%dn”, m ) ; return 0; } int max( int m, int n ) { if( m > n ) return m ; else return n ; } function
4. 4. Function Definition  Yes, you can do! 4 #include <stdio.h> int max( int m, int n ) { if( m > n ) return m ; else return n ; } int main(void) { int a, b, c, m ; scanf( “%d%d%d”, &a, &b, &c ) ; m = max( a, b ) ; printf( “%dn”, m ) ; ... } Function Invocation : call ‘max()’ Function Function Definition : definition of Function return value type function name parameter
5. 5. 5 Function Definition  Return value type – any type possible: char, int, float – if there is no return value: void  Function name – any name under naming convention  Parameters – number of parameters: any number – if there is no parameters: empty or void
6. 6. Function Definition  프로그램 수행 순서 1. 프로그램일 시작되면, main() 함수가 실행된다. 2. 함수호출을 만나면, main()함수의 실행이 멈 춘다. 3. Parameter가 함수쪽으로 복사된다. 4. 함수가 실행된다. 5. 함수에서 return을 만나 면, main()으로 돌아온다. 6. main()이 계속 수행된다. 6 #include <stdio.h> int max( int m, int n ) { if( m > n ) return m ; else return n ; } int main() { int m ; int a = 1 ; m = max( a, 10 ) ; printf( “%dn”, m ) ; }
7. 7. Function Definition  참고사항 – 함수가 또 다른 함수를 호 출할 수 있다. – 함수가 함수를 호출했을 때 수행 순서는, main()이 함수를 호출했을 때와 유 사하다. 7 #include <stdio.h> int max( int m, int n ) { if( m > n ) return m ; else return n ; } int max3( int a, int b, int c ) { int k = max( a, b ) ; k = max( k, c ) ; return k ; } int main() { int m ; m = max3( 3, 4, 10 ) ; printf( “%dn”, m ) ; }
8. 8. 8 Function Definition  The Return Statement – Stop function execution and return value – If there is no return value, you may use omit “return” #include <stdio.h> float abs(float x) { if(x>=0.0) return x; else return –x; } int main(void) { float y = -2.4 ; printf( “%fn”, abs(y) ) ; return 0; } #include <stdio.h> void PrintHello(void) { printf( “Hello, Worldn” ) ; } int main(void) { PrintHello(); return 0; } #include <stdio.h> void PrintHello(void) { printf( “Hello, Worldn” ); return ; printf( “Hellooooooon” ); } int main(void) { PrintHello(); return 0; }
9. 9. Example  What happens? 9 #include <stdio.h> int Sum( int k ) { int i, sum = 0 ; for( i = 1 ; i <= k ; i++ ) sum += i ; return sum ; } int main() { int i ; for( i = 0 ; i <= 10 ; i++ ) printf( “%dn”, Sum(i) ) ; return 0; } 0 1 3 6 10 15 21 28 36 45 55
10. 10. 10 Function Prototypes  Function Prototypes #include <stdio.h> double square(double x); int main(void) { int y=4; double result = square(y); printf(“square(%d) = %fn”, y, result); return 0; } double square(double x) { return x*x ; } #include <stdio.h> double square(double x) { return x*x ; } int main(void) { int y=4; double result = square(y); printf(“square(%d) = %fn”, y, result); return 0; }
11. 11. 11 Function Prototypes  Function Prototypes – Declaration saying “there is a function such as …” – Every function should be declared before it is used!!! – Format of declaration • return-type function_name ( parameter type list ); [Ex] double square(double); double square(double x) ;
12. 12. Recursive Problem Solving  Sum : Iterative version – What is the return value? 12 int sum ( int n ) { int sum = 0, k; for( k = 1 ; k <=n ; k++ ) sum += k; return sum; }
13. 13. Recursive Problem Solving  Sum : Recursive version – What is the return value? 13 int sum ( int n ) { if ( n == 1 ) return 1; else return n + sum( n – 1 ); }        1if 1if1 1 nna n a n n
14. 14. 14 Recursive Problem Solving  Sum : Recursive version – 호출이 몇 번 일어날까? int sum( int n ) ; int main() { printf( “%dn”, sum(3) ) ; return 0; } int sum ( int n ) { if ( n == 1 ) return 1; else return n + sum( n – 1 ) ; } main sum(3) sum(2) sum(1) return 1 return 2 +1 return 3 + 3call sum(3) call sum(2) call sum(1)
15. 15. 15 Recursive Problem Solving  Sum : Recursive version  sum의 변수 n은 몇 개가 생성될까? int sum( int n ) ; int main() { printf( “%dn”, sum(3) ) ; return 0; } int sum ( int n ) { if ( n == 1 ) return 1; else return n + sum( n – 1 ) ; } nsum(3) n n sum(3) sum(2) n n n sum(3) sum(2) sum(1) main()
16. 16. 16 Recursive Problem Solving  factorial을 구하는 예제 [Ex] /* Recursive version */ int fact( int n) { if ( n == 1 ) return 1; else return n * fact ( n - 1 ); } [Ex] /* Iterative version */ int fact( int n ) { int result = 1; for ( ; n > 1; --n ) result *= n; return result; }        1if 1if1 1 nna n a n n
17. 17. 17 Recursive Problem Solving  xn을 구하는 예제 [Ex] /* Recursive version */ int power ( int x, int n ) { if ( n == 0 ) return 1; else return x * power( x, n – 1 ); } [Ex] /* Iterative version */ int power ( int x, int n ) { int pow = 1; for ( ; n > 0; n--) pow *= x; return pow; }          1if1, 1if , nxnxa nx nxa
18. 18. 18 Local variables and Global variables  Local variables – All variables declared in a function including parameters – meaningful and useful only in the function body #include <stdio.h> void function() ; int main(void) { int i = 1, j = 0; function() ; printf(“main : i=%d, j=%d n”, i, j); return 0; } void function() { int j=0, k = 1; printf(“in function : j=%d, k=%d n”, j, k); } Same variables or Different variables with the same name?
19. 19. 19 Local variables and Global variables  Local variables #include <stdio.h> void function() ; int main(void) { int i = 1, j = 0; function() ; printf(“main : i=%d, j=%d n”, i, j); return 0; } void function() { int j=0, k = 1; printf(“in function : j=%d, k=%d n”, j, k); i = 0 ; } i j main j k function
20. 20. 20 Local variables and Global variables  Local variables – 함수의 파라미터도 해당 함수의 지역변수이다. #include <stdio.h> void function(int) ; int main(void) { int i = 0, j = 0; function(i, j) ; printf(“i=%d, j=%d n”, i, j); return 0; } void function(int i, int j) { int k = 0 ; i++ ; j++ ; printf(“i=%d, j=%d, k=%d n”, i, j, k); } i j main i j function k
21. 21. 21 Local variables and Global variables  Global variables – All variables declared outside functions – meaningful and useful in all program #include <stdio.h> int g = 0; int main(void) { int i =0, j=0 ; ... } void function(int i) { int j = 3 ; g++ ; } i j main i j function g your program
22. 22. 22 Local variables and Global variables  Global variables #include <stdio.h> void function(int i) ; int g = 0; int main(void) { int i =0, j=0 ; function(i) ; printf(“main : %d %d %dn”, i, j, g); return 0; } void function(int i) { int j = 3 ; i++ ; printf(“in function : %d %d %dn”, i, j, g); g++ ; } i j main i j function g your program
23. 23. 23 Invocation and Call-by-Value  Call-by-Value – When you call a function, only VALUES are passed to the function #include <stdio.h> void function(int k) { k ++ ; printf(“in function : k=%d n”, k); } int main(void) { int i = 1, j = 0; function(i); printf(“main : i=%d, j=%d n”, i, j); return 0; } i j main k function
24. 24. Invocation and Call-by-Value  Call-by-value 24 int main() { int i = 0, j = 1, k = 2 ; int temp ; printf( “%d %d %dn”, i, j, k ) ; temp = i ; i = j ; j = temp ; temp = j ; j = k ; k = temp ; printf( “%d %d %dn”, i, j, k ) ; return 0; } Can we replace this part with a function: Swap(x,y)
25. 25. Invocation and Call-by-Value  Call-by-value 25 #include <stdio.h> void Swap( int i, int j ); int main() { int i = 0, j = 1, k = 2 ; printf( “%d %d %dn”, i, j, k ) ; Swap( i, j ) ; Swap( j, k ) ; printf( “%d %d %dn”, i, j, k ) ; return 0; } void Swap( int i, int j ) { int temp ; temp = i ; i = j ; j = temp ; } OK ? Why not ? How to solve this?