120 likes | 233 Views
Functions / Procedures. In C, a function is equivalent to a procedure or a function in Pascal A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation
E N D
Functions / Procedures • In C, a function is equivalent to a procedure or a function in Pascal • A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation • Main idea: “To ignore how a job is done; knowing what is done is sufficient” • Functions we have used so far: printf, scanf, getchar, putchar • Statement structure return-type function-name (parameters declarations, if any) { declarations statements return expression; }
Simple functions #include <stdio.h> void prn_message(int n); void prn_message(int n) { int i; printf(“Message for you: “); for (i = 0; i < n; i++) printf(“%d. Cheers :D \n”, i); printf(“Have a nice day!\n”); } int main( ) { int n; printf(“How many CHEERS? “); scanf(“%d”, &n); prn_message(n); return 0; } #include <stdio.h> void prn_message( ); void prn_message( ) { printf(“Message for you: “); printf(“Have a nice day!\n”); } int main( ) { prn_message( ); return 0; }
Function power(x, y) #include <stdio.h> int power(int x, int y); /* declares the parameters types and names */ int main() { int i; for (i = 0; i < 10; i++) printf(“%d %d %d\n”, i, power(2, i), power(-3,i)); return 0; } /* Power: raise base to n-th power; n >=0 */ int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; i++) p = p * base; return p; }
Something related to functions • Main program is a function, too • Function definitions can appear in any order • What are parameters, formal & actual arguments • Formal arguments are local variables • The scope of the variables • A function need not return a value, a return statement with no expression cause control, but no useful value to be returned to the caller • The function-type -- the data type when the function returns • Return-types conversion
Functions C2F(int), and Function F2C(int) float C2F(int C) { float tmp; tmp = (C * 9.0 / 5.0) + 32.0; return tmp; } float F2C(int F) { float tmp; tmp = (F - 32.0) * 5.0 / 9.0; return tmp; } #include <stdio.h> /* Instead of float C2F(int x); */ float C2F(int); float F2C(int); int main() { float tmp; int i; for (i = 0; i < 100; i++){ tmp = C2F(i); printf(“%d C = %f F\n”, i, tmp); } for (i = 0; i < 213; i++){ tmp = F2C(i); printf(“%d F = %f C\n”, i, tmp); } return 0; }
The scope of the variables Output: i=2147307520 j=0 i=5 j=10 i=0 j=10 a=5 b=10 i=25 j=30 a=15 b=20 i=5 j=30 i=25 j=35 a=5 b=30 i=50 j=55 a=40 b=45 i=5 j=30 #include <stdio.h> void proc1(int a, int b); void proc2(int a, int b); int i = 0, j = 0; int main(){ int i; printf("i=%d j=%d\n", i, j); i = 5; j = 10; printf("i=%d j=%d\n", i, j); proc1(i, j); printf("i=%d j=%d\n", i, j); proc2(i, j); printf("i=%d j=%d\n", i, j); return 0; } void proc1(int a, int b){ printf("i=%d j=%d a=%d b=%d\n", i, j, a, b); a = 15; b = 20; i = 25; j = 30; printf("i=%d j=%d a=%d b=%d\n", i, j, a, b); } void proc2(int a, int b){ int j = 35; printf("i=%d j=%d a=%d b=%d\n", i, j, a, b); a = 40; b = 45; i = 50; j = 55; printf("i=%d j=%d a=%d b=%d\n", i, j, a, b); }
Arguments -- Call by value • In C, all function arguments are passed by value • The called function is given the values of its arguments in temporary variables rather than the originals #include <stdio.h> Program Output: void try_to_change(int x); i = 5 int main() x = 5 { x = 10 int i = 5; i = 5 printf(“i = %d\n”, i); try_to_change(i); printf(“i = %d\n”, i); return 0; } void try_to_change(int x) { printf(“x = %d\n”, x); x = 10; printf(“x = %d\n”, x); }
Advantage for call by value int power(int base, int n) int power(int base, int n) { { int i, p; int p; p = 1; for (p = 1; n > 0; n--) for (i = 1; i <= n; i++) p = p * base; p = p * base; return p; return p; } } Structure Programming (char arrays & function) Problem: To write a program that reads a set of text lines and prints the longest line out. Using Top-Down approach: 1. declarations and initializations 2. reading and text processing 3. printing out the longest line
Refinements • 2. Processing the text • 2.1 While there is another line do • 2.2 The corresponding actions when you find a longer line • 2.2 (refinement) • 2.2.1 if (it’s longer than the previous longest line) do • 2.2.1 (refinement) • 2.2.1.1 save its length • 2.2.1.2 save the line for later printing • After we all these refinements, we can build our program by substituting the smallest refinements by C code • Functions or procedures could be used to encapsulate some computations
Final Coding #include <stdio.h> int getline(char s[], int lim) #define MAXLINE 1000 /* max. input line size */ { int getline(char line[], int maxline); int c, i; void copy(char to[], char from[]); for (i = 0; i < lim-1 && /* print longest input line */ (c = getchar()) != EOF && int main() c != ‘\n’; i++) { s[i] = c; int len; if (c == ‘\n’){ int max; s[i] = c; char line[MAXLINE]; i++; char longest[MAXLINE]; } max = 0; s[i] = ‘\0’; while ((len = getline(line, MAXLINE)) > 0) return i; if (len > max){ } max = len; copy(longest, line); } if (max > 0) printf(“%s”, longest); return 0; } void copy(char to[], char from[]) { int i = 0; while ((to[i] = from[i]) != ‘\0’) i++; }
Function atoi() Converting a string to its number equivalent. (atoi) Steps: 1. Skip white space, if any 2. Get sign, if any 3. Get integer part and convert it #include <ctype.h> n = 0; int atoi(char s[]) while (isdigit(s[i])){ { n = n * 10 + (s[i] - ‘0’); int i, n, sign; i++; i = 0; } while (isspace(s[i])) i++; return sign * n; if (s[i] == ‘-’){ } sign = -1; i++; }else{ sign = 1; if (s[i] == ‘+’) i++; }
Function reverse() Reverse: reverse string S in place #include <string.h> void reverse(char s[]) { int c, i, j; i = 0; j = strlen(s) - 1; while ( i < j){ c = s[i]; s[i] = s[j]; s[j] = c; i++; j--; } }