Upcoming SlideShare
×

# 6. functions

355 views

Published on

Published in: Technology, Art & Photos
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
355
On SlideShare
0
From Embeds
0
Number of Embeds
111
Actions
Shares
0
5
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 6. functions

1. 1. Functions
2. 2. Function Overview #include <stdio.h> Read 3 numbers and print followings void main() { – max(first, second ) int a, b, c, m ; – max(first, third) – max(second, third) 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 ) ; } 2
3. 3. Function Overview Yes, you can do! int max( int m, int n ) #include <stdio.h> { if( m > n ) return m ; void main(void) else { return n ; int a, b, c, m ; } scanf( “%d%d%d”, &a, &b, &c ) ; m = max( a, b ) ; printf( “%dn”, m ) ; function m = max( a, c ) ; printf( “%dn”, m ) ; m = max( b, c ) ; printf( “%dn”, m ) ; } 3
4. 4. Function Definition Yes, you can do! #include <stdio.h> int max( int m, int n ) return value type { function name if( m > n ) return m ; parameter else return n ; Function Definition : } definition of Function void main(void) { int a, b, c, m ; scanf( “%d%d%d”, &a, &b, &c ) ; Function Invocation : call ‘max()’ Function m = max( a, b ) ; printf( “%dn”, m ) ; ... } 4
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 5
6. 6. Function Definition Program running #include <stdio.h> order 1. main() runs after int max( int m, int n ) { program start if( m > n ) 2. Stop main() running at return m ; function call else return n ; 3. Copy the parameter to } the function void main() 4. Start the function { 5. Return main() at return() int m ; a=1; 6. Resume main() m = max( a, 10 ) ; printf( “%dn”, m ) ; } 6
7. 7. #include <stdio.h>Function Definition int max( int , int n ) { Notes if( m > n ) – A function can call return m ; another function else return n ; – The running order when } a function call another function is similar when int max3( int a, int b, int c ) { main() call a function int k = max( a, b ) ; k = max( k, c ) ; return k ; } void main() { int m ; m = max3( 3, 4, 10 ) ; printf( “%dn”, m ) ; } 7
8. 8. Function Definition  The Return Statement – Stop function execution and return value – If there is no return value, you may omit “return”#include <stdio.h> #include <stdio.h>float abs(float x) #include <stdio.h>{ void PrintHello(void) if(x>=0.0) void PrintHello(void) { return x; { printf( “Hello, Worldn” ); else printf( “Hello, Worldn” ) ; return ; return –x; }} printf( “Hellooooooon” ); void main(void) }void main(void) {{ PrintHello(); void main(void) float y = -2.4 ; } { printf( “%fn”, y, abs(y) ) ; PrintHello();} } 8
9. 9. Example What happens? 0 #include <stdio.h> 1 3 6 int Sum( int k ) 10 { 15 int i, sum = 0 ; 21 28 for( i = 1 ; i <= k ; i++ ) 36 sum += i ; 45 return sum ; 55 } void main() { int i ; for( i = 0 ; i <= 10 ; i++ ) printf( “%dn”, Sum(i) ) ; } 9
10. 10. Function Prototypes  Function Prototypes #include <stdio.h>#include <stdio.h> double square(double x);double square(double x){ int main(void) return x*x ; {} int y=4; double result = square(y);int main(void){ printf(“square(%d) = %fn”, result); int y=4; } double result = square(y); double square(double x) printf(“square(%d) = %fn”, result); {} return x*x ; } 10
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) ; 11
12. 12. Recursive Problem Solving Sum : Iterative version – What is the return value? int sum ( int n ) { int sum = 0, k; for( k = 1 ; k <=n ; k++ ) sum += k; return sum; } 12
13. 13. Recursive Problem Solving Sum : Recursive version – What is the return value? int sum ( int n ) { if ( n == 1 )  1 if n  1 an   return 1; an 1  n if n  1 else return n + sum( n – 1 ); } 13
14. 14. Recursive Problem Solving Sum : Recursive version – How many calls? main int sum( int n ) ; call sum(3) return 3 + 3 void main() { sum(3) printf( “%dn”, sum(3) ) ; } call sum(2) return 2 +1 int sum ( int n ) { sum(2) if ( n == 1 ) return 1; call sum(1) return 1 else return n + sum( n – 1 ) ; sum(1) } 14
15. 15. Recursive Problem Solving Sum : Recursive version  How many variables are created? sum(1) nint sum( int n ) ; sum(2) nvoid main() sum(3) n{ printf( “%dn”, sum(3) ) ;} sum(2) n sum(3) nint sum ( int n ){ if ( n == 1 ) return 1; sum(3) n else return n + sum( n – 1 ) ;} main() 15
16. 16. Recursive Problem Solving A factorial example[Ex] /* Recursive version */ [Ex] /* Iterative version */int fact( int n) int fact( int n ){ { if ( n == 1 ) int result = 1; return 1; else for ( ; n > 1; --n ) return n * fact ( n - 1 ); result *= n;} return result; }  1 if n  1 an   an 1  n if n  1 16
17. 17. Recursive Problem Solving An xn example [Ex] /* Recursive version */ [Ex] /* Iterative version */ int power ( int x, int n ) int power ( int x, int n ) { { if ( n == 0 ) int pow = 1; return 1; for ( ; n > 0; n--) else pow *= x; return x * power( x, n – 1 ); return pow; } }  x if n  1 ax, n    ax, n  1 x if n  1 17
18. 18. Invocation and Call-by-Value Call-by-Value – When you call a function, only VALUES are passed to the function #include <stdio.h> main void function(int k) { i j k ++ ; printf(“in function : k=%d n”, k); } int main(void) { function int i = 1, j = 0; k function(i); printf(“main : i=%d, j=%d n”, i, j); } 18
19. 19. Invocation and Call-by-Value Call-by-valuevoid main(){ int i = 0, j = 1, k = 2 ; int temp ; Can we replace this part with a function: printf( “%d %d %dn”, i, j, k ) ; Swap(x,y) temp = i ; i=j; j = temp ; temp = j ; j=k; k = temp ; printf( “%d %d %dn”, i, j, k ) ;} 19
20. 20. Invocation and Call-by-Value Call-by-valuevoid main() void Swap( int i, int j ){ { int i = 0, j = 1, k = 2 ; int temp ; printf( “%d %d %dn”, i, j, k ) ; temp = i ; i=j; Swap( i, j ) ; j = temp ; Swap( j, k ) ; } printf( “%d %d %dn”, i, j, k ) ; OK ? Why not ?} How to solve this? 20