beginning c programming for engineers
play

Beginning C Programming for Engineers Lecture 4: Functions R. - PowerPoint PPT Presentation

Beginning C Programming for Engineers Lecture 4: Functions R. Lindsay Todd Functions p. 1/19 C Preprocessor The C Preprocessor includes header files like stdio.h , expands macros like RAND_MAX , and handles conditional compilation .


  1. Beginning C Programming for Engineers Lecture 4: Functions R. Lindsay Todd Functions – p. 1/19

  2. C Preprocessor The C Preprocessor includes header files like stdio.h , expands macros like RAND_MAX , and handles conditional compilation . preprocessor compiler prog.c prog #include <math.h> #ifndef PI #define PI 3.1415926536 #endif Functions – p. 2/19

  3. Functions Functions are “subprograms” with parameters, program statements, and a result. They are used to: Break a program into simpler pieces. Consolidate similar code used in several places in a program. Reuse the same code in more than one program. (Groups of reusable functions are often organized as libraries .) Link to code written in other programming languages. Functions – p. 3/19

  4. Prototypes and Definitions #include <stdio.h> 1 A prototype tells the 2 /* Prototype */ 3 compiler that there is a float Square( float x); 4 5 function with a /* Main function */ 6 int 7 specified name, return main() 8 { 9 type, and parameters. printf("%g, %g, %g\n", 10 Square(1.), Square(2.), Square(3.)); 11 return 0; The function definition 12 } 13 is like the prototype, 14 /* The function */ 15 float 16 but also includes the Square( float x) 17 { 18 body of the function. return x*x; 19 } 20 1, 4, 9 Functions – p. 4/19

  5. Function: Radian 1 #include <stdio.h> #include <math.h> 2 3 #ifndef PI 4 #define PI 3.14159 5 6 #endif 7 8 /* Prototype */ Enter angle (degrees): 45 9 float Radian( float x); 10 Enter adjacent side: 60 11 /* Main function */ 12 int main() { Opposite side: 59.9999 float ang, adj, opp; 13 14 printf("Enter angle (degrees): "); 15 scanf("%g", &ang); printf("Enter adjacent side: "); 16 17 scanf("%g", &adj); Enter angle (degrees): 60 opp = adj * tan(Radian(ang)); 18 printf("Opposite side: %g\n", opp); 19 Enter adjacent side: 10 20 return 0; } 21 Opposite side: 17.3205 22 23 /* The function */ float 24 25 Radian( float deg) 26 { float result; 27 28 result = deg/180.0 * PI; 29 return result; } 30 Functions – p. 5/19

  6. Modularization 1 #include <stdio.h> 2 #include <stdlib.h> Break a program into #include <time.h> 3 4 logically simpler 5 int RandomInt( int m); int SquareInt( int x); components. 6 7 8 Program each of those 9 int main() { 10 components. 11 int rs; 12 srand(time(NULL)); Combine components into rs = SquareInt( RandomInt(10) ); 13 14 printf("Random square: %d\n", rs); one program. 15 return 0; } 16 17 18 int RandomInt( int m) Random square: 81 { 19 return 1 + rand() % m; 20 21 } 22 Random square: 49 int SquareInt( int x) 23 24 { 25 return x*x; } 26 Functions – p. 6/19

  7. Flowchart: Summing Primes Get limit i = 2 sum = 0 ++i n = 2 No i < n ++n Yes No No Print sum n <= limit ? n % i == 0 Yes Yes No return 0 Is n prime? Yes sum += n return 1 Functions – p. 7/19

  8. Summing Primes #include <stdio.h> 1 2 /* Return 1 if prime, 0 otherwise. */ 1 int isPrime( int n); 3 4 2 int main() { 5 int isPrime( int n) { 3 int n, limit, sum = 0; 6 int i; 4 printf("Enter limit: "); 7 for (i = 2; i < n; ++i) { 5 scanf("%d", &limit); 8 if (n % i == 0) { for (n = 2; n <= limit; ++n) { 9 6 if (isPrime(n)) { 10 return 0; 7 sum += n; 11 } 8 } 12 } 9 } 13 printf("Sum of primes <= %d is %d\n", return 1; 14 10 limit, sum); 15 } 11 return 0; 16 } 17 Enter limit: 1000 Sum of primes <= 1000 is 76127 Functions – p. 8/19

  9. Comparing Summing Programs #include <stdio.h> 1 2 int isPrime( int n); 3 4 int main() { 5 /* Sum up primes. */ int n, limit, sum = 0; 1 6 printf("Enter limit: "); 7 2 scanf("%d", &limit); #include <stdio.h> 8 3 for (n = 2; n <= limit; ++n) { 9 4 if (isPrime(n)) { 10 int main() 5 sum += n; 11 { 6 } 12 int i, n, limit, sum = 0; 7 } 13 printf("Enter limit: "); 8 printf("Sum of primes <= %d is %d\n", 14 scanf("%d", &limit); 9 limit, sum); 15 for (n = 2; n <= limit; ++n) { 10 return 0; 16 for (i = 2; i < n; ++i) { 11 } 17 if (n % i == 0) break ; 12 } 13 /* Return 1 if prime, 0 otherwise. */ 1 if (i == n) sum += n; 14 2 } 15 int isPrime( int n) { 3 printf("Sum of primes <= %d is %d\n", 16 int i; 4 limit, sum); 17 for (i = 2; i < n; ++i) { 5 return 0; 18 if (n % i == 0) { 6 } 19 return 0; 7 } 8 } 9 return 1; 10 } 11 Functions – p. 9/19

  10. Primes and Reverses 1 /* Test n, reverse(n) for primeness. */ Get limit #include <stdio.h> 2 int isPrime( int n); 3 n = 2 int reverse( int n); 4 ++n 5 6 int main() No 7 { n <= limit ? int limit, n, r; 8 Yes printf("Enter limit: "); 9 No Is n prime? scanf("%d", &limit); 10 11 for (n = 2; n <= limit; ++n) { Yes 12 if (!isPrime(n)) continue ; r = reverse(n) r = reverse(n); 13 if (!isPrime(r)) continue ; 14 No Is r prime? printf("%d and %d are prime.\n", n, r); 15 } 16 Yes 17 return 0; Print n , r 18 } Functions – p. 10/19

  11. Comparing Reverse Prime /* Test n, reverse(n) for primeness. */ 1 #include <stdio.h> 2 int isPrime( int n); 3 int reverse( int n); 4 5 /* Test n, reverse(n) for primeness. */ int main() 1 6 #include <stdio.h> 2 { 7 int reverse( int n); int limit, n, r; 3 8 printf("Enter limit: "); 4 9 int main() 5 scanf("%d", &limit); 10 { for (n = 2; n <= limit; ++n) { 6 11 int i, limit, n, r; if (!isPrime(n)) continue ; 7 12 printf("Enter limit: "); 8 r = reverse(n); 13 scanf("%d", &limit); if (!isPrime(r)) continue ; 9 14 for (n = 2; n <= limit; ++n) { printf("%d and %d are prime.\n", n, r); 10 15 for (i = 2; i < n; ++i) { 11 } 16 if (n % i == 0) break ; return 0; 12 17 } 13 } 18 if (i != n) continue ; /* Not prime */ 14 r = reverse(n); 15 /* Return 1 if prime, 0 otherwise. */ 1 for (i = 2; i < r; ++i) { 16 2 if (r % i == 0) break ; 17 int isPrime( int n) { 3 } 18 int i; if (i != r) continue ; /* Not prime */ 4 19 printf("%d and %d are prime.\n", n, r); 20 for (i = 2; i < n; ++i) { 5 } 21 if (n % i == 0) { 6 return 0; 22 return 0; 7 } 23 } 8 } 9 return 1; 10 } 11 Functions – p. 11/19

  12. More than one parameter Functions may also have more than one parameter. #include <stdio.h> 1 2 float RectArea( float length, float width); 3 4 int 5 main() 6 Length, width: 4, 5 { 7 float x, y, area; 8 Area = 20 printf("Length, width: "); 9 scanf("%g, %g", &x, &y); 10 area = RectArea(x, y); 11 Length, width: 12, 0.5 printf("Area = %g\n", area); 12 return 0; 13 Area = 6 } 14 15 float 16 RectArea( float length, float width) 17 { 18 return length * width; 19 } 20 Functions – p. 12/19

  13. Miscellaneous Details Libraries often have one or more header files , e.g., stdio.h , containing function prototypes. Functions that take no parameters should have a parameter list containing only the keyword void . Functions that do not return a value should declared to have a return type of void . (Such functions may be useful for their “side effects”.) Examples: void exit( int exitCode); void abort( void ); int rand( void ); Functions – p. 13/19

  14. Variable scope Each function has its own scope for variables, so variables with the same name do not clash. A function may only use variables it declares itself (including parameters). Both main and #include <stdio.h> OddEvenTest contain a 1 2 variable named a . These int OddEvenTest( int b); 3 4 are different memory int main() { 5 int a, result; 6 objects. 7 a = 5; 8 result = OddEvenTest(a); Memory objects are al- 9 printf("a = %d; result = %d\n", a, result); 10 located when a function return 0; 11 } 12 starts, and go away when 13 int OddEvenTest( int b) { 14 it ends. int a; 15 a = b % 2; 16 return a; 17 a = 5; result = 1 } 18 Functions – p. 14/19

  15. Scope: Activation Records #include <stdio.h> 1 2 int OddEvenTest( int b); 3 4 5 a main int main() { 5 int a, result; 6 7 a = 5; 8 result = OddEvenTest(a); 9 printf("a = %d; result = %d\n", a, result); 10 return 0; 11 } 12 Memory before calling 13 int OddEvenTest( int b) { 14 OddEvenTest . int a; 15 a = b % 2; 16 return a; 17 } 18 Functions – p. 15/19

  16. Scope: Activation Records #include <stdio.h> 1 2 int OddEvenTest( int b); 3 4 5 a main int main() { 5 int a, result; 6 5 b OddEvenTest 1 a 7 a = 5; 8 result = OddEvenTest(a); 9 printf("a = %d; result = %d\n", a, result); 10 return 0; 11 } 12 Memory while OddEvenTest 13 int OddEvenTest( int b) { 14 is active. int a; 15 a = b % 2; 16 return a; 17 } 18 Functions – p. 15/19

  17. Scope: Activation Records #include <stdio.h> 1 2 int OddEvenTest( int b); 3 4 5 a main int main() { 5 int a, result; 6 7 a = 5; 8 result = OddEvenTest(a); 9 printf("a = %d; result = %d\n", a, result); 10 return 0; 11 } 12 Memory after OddEvenTest 13 int OddEvenTest( int b) { 14 returns. int a; 15 a = b % 2; 16 return a; 17 } 18 Functions – p. 15/19

Recommend


More recommend