Download presentation
Presentation is loading. Please wait.
1
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; }
2
Simple functions #include <stdio.h> void prn_message( );
{ printf(“Message for you: “); printf(“Have a nice day!\n”); } int main( ) prn_message( ); return 0; #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;
3
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;
4
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
5
Functions C2F(int), and Function F2C(int)
#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; float C2F(int C) { float tmp; tmp = (C * 9.0 / 5.0) ; return tmp; } float F2C(int F) tmp = (F ) * 5.0 / 9.0;
6
The scope of the variables
#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; proc1(i, j); proc2(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; void proc2(int a, int b){ int j = 35; a = 40; b = 45; i = 50; j = 55; Output: i= 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
7
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); return 0; } void try_to_change(int x) { printf(“x = %d\n”, x); x = 10;
8
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
9
Refinements 2. Processing the text 2.2 (refinement) 2.2.1 (refinement)
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) save its length 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
10
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++;
11
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++; }
12
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--; }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.