דוגמה - פונקציות כתוב פונקציה המחשבת ומחזירה את המספר הגדול ביותר בין שלושת המספרים השלמים : המועברים לפונקציה כפרמטרים • • • • • #include<stdio. h> int max 3(int, int); //function prototype void main() { int a, b, c; //input variables printf("enter 3 integers : "); scanf("%d%d%d", &a, &b, &c); printf("maximum integer is : %dn", max 3(a, b, c)); } int max 3(int x, int y, int z) //function definition { int max; //help variable max=x; if(y>max) max=y; if(z>max) max=z; return max; }
דוגמה - פונקציות . מחשבת ומדפיסה את המספר הגדול ביותר בין כל המספרים הנקלטים , מספרים שלמים 30 כתוב תוכנית הקולטת . • • • #include<stdio. h> int max 3(int, int); //function prototype void main() { int a, b, c, i, max; scanf("%d%d%d", &a, &b, &c); max= max 3(a, b, c); for (i=1; i<10; i++) { scanf("%d%d%d", &a, &b, &c); if (max 3(a, b, c)>max) max= max 3(a, b, c); } printf ("max= %dn", max); } /*Here we will place the implementation of max 3*/ max 3 נשתמש בפונקציה
: דוגמאות למימוש הפונקציות לטיפול במחרוזות /********************************* • Parameters: s - array of chars, must contain ‘ ’. • Returns: length of string that s holds • **********************************/ • unsigned int strlen(char s[]) • { • unsigned int len = 0; • while (s[len] != ’ ’) • len++; • return len; • } • /********************************** • Compares two strings lexicographically • Parameters: s, t - string to be compared • Returns: lexicographical difference between s and t • **********************************/ • int strcmp(char s[], char t[]) • { • int i; • for (i = 0; s[i] == t[i]; i++) • if (s[i] == '