100 likes | 216 Views
Chapter 11. Functions. 11.1 Functions. Why functions? sin(x),sqrt(x), … Structural programming Maintenance. 11.2 System Functions. Pseudo Number Generator #include <stdlib.h> rand(), srand(),randomize() Numerical #include <math.h>
E N D
Chapter 11 Functions Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.1 Functions • Why functions? • sin(x),sqrt(x), … • Structural programming • Maintenance Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.2 System Functions • Pseudo Number Generator • #include <stdlib.h> • rand(), srand(),randomize() • Numerical • #include <math.h> • fabs(), pow(), sqrt(), sin(), cos(), tan(), asin(), acos(), atan(), exp(), log(), log10(), ceil(), floor() Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.2 System Functions (cont) • Clock • #include <time.h> • clock(), time() Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.3 User Defined Functions int sum(int i, int j) { int k; k=i+j; return(k); } void main(void) { printf(“%d”,sum(5,4)); } Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.4 Parameter Passing • Call-by-Value int sum(int i, int j) { int k; k=i+j; return(k); } void main(void) { int x=5; int y=4; printf(“%d”,sum(x,y)); } Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.4 Parameter Passing (cont) • Call-by-Address void inc(int i) void inc(int* ip) { { i++; (*ip)++; } } void main(void) void main(void) { { int i=1; int i=1; inc(i); inc(&i); printf(“%d”,i); printf(“%d”,i); } } Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.5 Passing Array as Parameter void sort(int x[10]) { … } void main(void) { int x[10]={5,7,3,45,8,5,4,24,67,23}; sort(x); } Windows Programming, C.-S. Shieh, KUAS EC, 2005
11.6 Recursive Functions • Recursive Function • Recursive Relation • Terminate Condition • Fabonacci function long fabo(long i) { if(i==0) return 0; if(i==1) return 1; if(i>=2) return fabo(i-1)+fabo(i-2); } Windows Programming, C.-S. Shieh, KUAS EC, 2005
Console Applications • Develop console applications using Borland C++Builder • FileNew…Console Wizard Windows Programming, C.-S. Shieh, KUAS EC, 2005