1 / 32

The 5 th lecture Jiří Šebesta

Computers and programming. The 5 th lecture Jiří Šebesta. TOPIC. Functions - introduction Headers of functions Recursion of functions Libraries of functions Example. Functions – introduction (1/3). Application: repetitively executed sequence of statements structuring a program

Download Presentation

The 5 th lecture Jiří Šebesta

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computers and programming The5th lecture Jiří Šebesta

  2. TOPIC Functions - introduction Headers of functions Recursion of functions Libraries of functions Example

  3. Functions – introduction (1/3) • Application: • repetitively executed sequence of statements • structuring a program • events handling Function:a sequence of statements completed by a header

  4. Functions – introduction (2/3) What is happen, if the function is called ?

  5. Functions – introduction (3/3) • The basic types of functions: • - functions without parameter • - functions with parameter • call by value(it do not change the content of an input variable, which is declared in the superior code) • call by reference(a pointer is hand in function as a parameter, the content of input parameter can be changed) • function with/without returning value

  6. A function without a parameter and without a returning value: Headers of functions (1/13) void func_name(void){ } voidwarning(void) // no input, no output { printf("Your computer is beingdestroyed"); getchar(); } void main(void) { warning(); // calling the function }

  7. A function with one parameter called as a value and with a returning value: Headers of functions (2/13) double func_name(double A){ } intpow3(int A) // 1 input, 1 output { int y = A*A*A; return y; } void main(void) { int n; for(n=0; n<20; n++) printf(“%d^3 is %d\n“, n, pow3(n)); }

  8. A function with two parameters called as values and with a returning value: Headers of functions (3/13) double func_name(double A, double B) { } The function for n-power of rational number floatpow_n(float A, int B)// 2 inputs, 1 output { float y = 1; int n; for(n=0; n<=B; n++) if(n>0) y *= A; return y; }

  9. Headers of functions (4/13) int main(void) { int n; float y[9]; float m = 5.123; for(n=2; n<11; n++) { y[n-2] = pow_n(m, n); printf("%dth power of %f is %f\n",n,m,y[n-2]); } getchar(); return 0; } Source code: Ex51.c

  10. A function with a parameter as a string Headers of functions (5/13) ??? func_name(char* A) { } The function returning number of ciphers in the string intnuminstr(char *s) { int n, count=0; for(n=0; s[n]!='\0'; n++) if(s[n]>='0'&&s[n]<='9') count++; return count ; }

  11. Headers of functions (6/13) int main(void) { chartxt1[] = "ab9bj65D9"; chartxt2[] = "34x9z56A0"; char txt3[] = "3cvz1111E"; int n1, n2, n3; n1 = numinstr(txt1); n2 = numinstr(txt2); n3 = numinstr(txt3); printf("%d in 1st, %d in 2nd, %d in 3rd", n1, n2, n3); getchar(); return 0; } Source code: Ex52.c

  12. A function with parameters called as reference Headers of functions (7/13) ??? func_name(int *A, int *B) { } • Parameters are pointers The function for conversion from polar to Cartesian coordinates voidpolar2cart(float *A, float *B)// A is magnitude {// B is phase in deg. float pha; pha = 3.1415 * *B/180; *B = *A * sin(pha); // B is imag. part *A = *A * cos(pha); // A is real part }

  13. Headers of functions (8/13) int main(void) { float x=2.0; float y=90.0; printf("Magnitude is %fand phase is %f deg.", x, y); polar2cart(&x, &y); printf("Real part is %fand imag. part is %f", x, y); getchar(); return 0; } Source code: Ex53.c

  14. A function with parameter, which references the pointer to the first element of array (alike for strings) Headers of functions (9/13) The function for mean value calculation from vector of rational numbers floatmean(float *vect, int n)// vect is input vector {// n is length of the vector int m, float s=0; // s is partial sum for(m=0; m<n; m++) // for all elements of vector s += vect[m]; return s/(1.0*n); // average computation }

  15. Headers of functions (10/13) int main(void) { float x[5], m; int len_x=5, n; srand(time(NULL)); for(n=0; n<len_x; n++) { x[n]=(rand()%1000)/100.0; printf("%4.2f\n", x[n]); } m = mean(x, len_x); printf("The average of vector of number is %f", m); getchar(); return 0; } Source code: Ex54.c

  16. Own type of variable can be defined by usingtypedef Headers of functions (11/13) typedef int t_mat[3][3] ; void func_name(t_mat A, t_matB) { } int func_name(t_mat A) { } t_mat func_name(t_mat A) { } !!! A function can return a value or pointer only (no array) !!!

  17. The function for determinant calculation from matrix 3 by 3 Headers of functions (12/13) typedef int t_mat[3][3]; intdet_mat(t_matA)// determinant of 3x3 matrix { intdet=0, m, n, pplus, pminus; for(m=0; m<3; m++) { pplus=1; pminus=1; for (n=0; n<3; n++) { pplus*=A[(m+n)%3][n]; pminus*=A[(m+n)%3][2-n]; } det+=pplus-pminus; } return det; }

  18. int main(void) { t_mat mat; int m, n; srand(time(NULL)); for(m=0; m<3; m++) for(n=0; n<3; n++) mat[m][n]=rand()%198-99; printf("The determinant is %d", det_mat(mat)); getchar(); return 0; } Headers of functions (13/13) Source code: Ex55.c

  19. A function can call the same function in its body Recursion of function (1/1) int func_name(intA) {… x = func_name(y); …} The function for factorial computation unsigned intfactorial(unsigned int n) { if(n) //test if n is not zero return n*factorial(n-1); //n is not zero else// n is zero return 1; } Source code: Ex56.c

  20. Libraries of functions (1/10) • A library can be build-up using own set of functions: • my_lib.h:a file containing headers of our functions from the same-name file my_lib.c:a file containing source codes of our functions • #include”my_lib.h”:have to beinserted in our program, in whichfunctions form the library file my_lib.care called

  21. Libraries of functions (2/10) • In Code::Blocks: File > New > File my_lib.c +my_lib.hinto the directory of the project or specific own directory for own libraries

  22. Libraries of functions (3/10) • Setting of paths for compiler in Code::Blocks: • Settings > Compiler add paths with sources and headers

  23. Own library of functions for operations with 3 by 3 matrixes Libraries of functions (4/10) typedefint t_mat[3][3]; int det_mat(t_mat A);// determinant of A void print_mat(t_mat A);// printing A void gen_mat(t_mat A, int start, int stop);// gener. A void copy_mat(t_mat A, t_mat B);// copying A to B void add_mat(t_mat A, t_mat B);// A=A+B void sub_mat(t_mat A, t_mat B);// A=A-B void clr_mat(t_mat A);// zeroizing A header filematops.h • own types of variables definitions • header functions with definitions of types for all input and output parameters

  24. Libraries of functions (5/10) • it includes source codes for all functions according to header filematops.h #include<stdio.h> #include<time.h> #include<stdlib.h> typedefint t_mat[3][3]; int det_mat(t_mat A)// compute determinant { int det=0, m, n, pplus, pminus; for(m=0; m<3; m++) { source codes of librarymatops.c

  25. pplus=1; pminus=1; for (n=0; n<3; n++) { pplus*=A[(m+n)%3][n]; pminus*=A[(m+n)%3][2-n]; } det+=pplus-pminus; } return det; } Libraries of functions (6/10)

  26. void print_mat(t_mat A)// printing matrix A { int m,n; for(m=0; m<3; m++) { for(n=0; n<3; n++) { printf("%4d ", A[m][n]); } printf("\n"); } } void gen_mat(t_mat A, int start, int stop)// random { int m, n;// generation of matrix A srand(time(NULL)); for(m=0; m<3; m++) for(n=0; n<3; n++) A[m][n]=rand()%(stop-start+1)+start; } Libraries of functions (7/10)

  27. void copy_mat(t_mat A, t_mat B)// copying A to B { int m, n; for(m=0; m<3; m++) for(n=0; n<3; n++) A[m][n]=B[m][n]; } void add_mat(t_mat A, t_mat B)// A=A+B { int m, n; for(m=0; m<3; m++) for(n=0; n<3; n++) A[m][n]+=B[m][n]; } Libraries of functions (8/10)

  28. void sub_mat(t_mat A, t_mat B)// A=A-B { int m, n; for(m=0; m<3; m++) for(n=0; n<3; n++) A[m][n]-=B[m][n]; } void clr_mat(t_mat A)// zeroizing A { int m,n; for(m=0; m<3; m++) for(n=0; n<3; n++) A[m][n]=0; } Libraries of functions (9/10)

  29. Example of a program, which uses ownlibrarymatops.h Libraries of functions (10/10) #include<stdio.h> #include"matops.h" int main(void) { t_mat mat1, mat2, mat3;// this type was defined//in mathops.h int m, n; gen_mat(mat1, -9, 9); print_mat(mat1); printf("The det. of mat1 is %d\n",det_mat(mat1)); copy_mat(mat3,mat1); sub_mat(mat1,mat2); … Source code: Ex57.c + matops.c(h)

  30. Example (1/2) Create a library with functions for computing areas of 2-D geometric objects (build-up functions for rectangle, ellipse and triangle). • Header file for libraryareas2D.h doubles_rect(doublea, doubleb); double s_ell (doublea, doubleb); doubles_tri (doublea, doubleb);

  31. Library functions inareas2D.cpp Example (2/2) #include "areas2D.h" doubles_rect(doublea, doubleb) { return a*b;} double s_ell (doublea, doubleb) { return 3.1415*a*b;} doubles_tri (doublea, doubleb) { return 0.5*a*b;} Source code: Ex58.c + areas2D.c(h)

  32. TOPIC OF THE NEXT LECTURE Advanced data types THANK YOUFOR YOUR ATTENTION

More Related