The 5 th lecture Jiří Šebesta

32
Computers and programming The 5 th lecture Jiří Šebesta

description

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 - PowerPoint PPT Presentation

Transcript of The 5 th lecture Jiří Šebesta

Page 1: The 5 th  lecture Jiří Šebesta

Computers and programming

The 5th lecture

Jiří Šebesta

Page 2: The 5 th  lecture Jiří Šebesta

TOPIC

1. Functions - introduction2. Headers of functions 3. Recursion of functions4. Libraries of functions5. Example

Page 3: The 5 th  lecture Jiří Šebesta

Functions – introduction (1/3)

• Function: a sequence of statements completed by a header

• Application:– repetitively executed sequence of statements– structuring a program– events handling

Page 4: The 5 th  lecture Jiří Šebesta

Functions – introduction (2/3)

• What is happen, if the function is called ?

Page 5: The 5 th  lecture Jiří Šebesta

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

Page 6: The 5 th  lecture Jiří Šebesta

Headers of functions (1/13)

void func_name(void){ }

void warning(void) // no input, no output{ printf("Your computer is being destroyed"); getchar();}

void main(void){ warning(); // calling the function}

• A function without a parameter and without a returning value:

Page 7: The 5 th  lecture Jiří Šebesta

Headers of functions (2/13)

double func_name(double A){ }int pow3(int A) // 1 input, 1 output{ int y = A*A*A; return y;}

• A function with one parameter called as a value and with a returning value:

void main(void){ int n; for(n=0; n<20; n++) printf(“%d^3 is %d\n“, n, pow3(n));}

Page 8: The 5 th  lecture Jiří Šebesta

Headers of functions (3/13)

double func_name(double A, double B) { }

float pow_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;}

• A function with two parameters called as values and with a returning value:

The function for n-power of rational number

Page 9: The 5 th  lecture Jiří Šebesta

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

Page 10: The 5 th  lecture Jiří Šebesta

Headers of functions (5/13)

??? func_name(char* A) { }

int numinstr(char *s){ int n, count=0;

for(n=0; s[n]!='\0'; n++) if(s[n]>='0'&&s[n]<='9')

count++;

return count ;}

• A function with a parameter as a string

The function returning number of ciphers in the string

Page 11: The 5 th  lecture Jiří Šebesta

Headers of functions (6/13)

int main(void){ char txt1[] = "ab9bj65D9"; char txt2[] = "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

Page 12: The 5 th  lecture Jiří Šebesta

Headers of functions (7/13)

??? func_name(int *A, int *B) { }

void polar2cart(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 }

• A function with parameters called as reference

• Parameters are pointers

The function for conversion from polar to Cartesian coordinates

Page 13: The 5 th  lecture Jiří Šebesta

Headers of functions (8/13)

int main(void){ float x=2.0; float y=90.0;

printf("Magnitude is %f and phase is %f deg.", x, y);

polar2cart(&x, &y); printf("Real part is %f and imag. part is %f", x, y);

getchar(); return 0;}

Source code: Ex53.c

Page 14: The 5 th  lecture Jiří Šebesta

Headers of functions (9/13)

float mean(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 }

• A function with parameter, which references the pointer to the first element of array (alike for strings)

The function for mean value calculation from vector of rational numbers

Page 15: The 5 th  lecture Jiří Šebesta

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

Page 16: The 5 th  lecture Jiří Šebesta

Headers of functions (11/13)

typedef int t_mat[3][3] ;

void func_name(t_mat A, t_mat B) { }

• Own type of variable can be defined by using typedef

int func_name(t_mat A) { }

t_mat func_name(t_mat A) { }

!!! A function can return a value or pointer only (no array) !!!

Page 17: The 5 th  lecture Jiří Šebesta

Headers of functions (12/13)

typedef int t_mat[3][3];int det_mat(t_mat A) // determinant of 3x3 matrix { int det=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; }

The function for determinant calculation from matrix 3 by 3

Page 18: The 5 th  lecture Jiří Šebesta

Headers of functions (13/13)

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;}

Source code: Ex55.c

Page 19: The 5 th  lecture Jiří Šebesta

Recursion of function (1/1)

unsigned int factorial(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;}

int func_name(int A) {…

x = func_name(y);

…}

• A function can call the same function in its body

Source code: Ex56.c

The function for factorial computation

Page 20: The 5 th  lecture Jiří Šebesta

Libraries of functions (1/10)

• my_lib.c: a file containing source codes of our functions

• my_lib.h: a file containing headers of our functions from the same-name file

• #include ”my_lib.h”: have to be inserted in our program, in which functions form the library file my_lib.c are called

• A library can be build-up using own set of functions:

Page 21: The 5 th  lecture Jiří Šebesta

Libraries of functions (2/10)

• In Code::Blocks: File > New > File

• my_lib.c + my_lib.h into the directory of the project or specific own directory for own libraries

Page 22: The 5 th  lecture Jiří Šebesta

Libraries of functions (3/10)

• Setting of paths for compiler in Code::Blocks:

Settings > Compiler

• add paths with sources and headers

Page 23: The 5 th  lecture Jiří Šebesta

Libraries of functions (4/10)

• Own library of functions for operations with 3 by 3 matrixes

• header file matops.h

typedef int t_mat[3][3];int det_mat(t_mat A); // determinant of Avoid print_mat(t_mat A); // printing A void gen_mat(t_mat A, int start, int stop); // gener. Avoid copy_mat(t_mat A, t_mat B); // copying A to Bvoid add_mat(t_mat A, t_mat B); // A=A+Bvoid sub_mat(t_mat A, t_mat B); // A=A-Bvoid clr_mat(t_mat A); // zeroizing A

• own types of variables definitions• header functions with definitions of types for all input and output parameters

Page 24: The 5 th  lecture Jiří Šebesta

Libraries of functions (5/10)

• source codes of library matops.c

#include <stdio.h>#include <time.h>#include <stdlib.h>

typedef int 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++){

• it includes source codes for all functions according to header file matops.h

Page 25: The 5 th  lecture Jiří Šebesta

Libraries of functions (6/10)

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; }

Page 26: The 5 th  lecture Jiří Šebesta

Libraries of functions (7/10)

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;}

Page 27: The 5 th  lecture Jiří Šebesta

Libraries of functions (8/10)

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];

}

Page 28: The 5 th  lecture Jiří Šebesta

Libraries of functions (9/10)

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;

}

Page 29: The 5 th  lecture Jiří Šebesta

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);…

• Example of a program, which uses own library matops.h

Source code: Ex57.c + matops.c(h)

Page 30: The 5 th  lecture Jiří Šebesta

Example (1/2)

Create a library with functions for computing areas of 2-D geometric objects (build-up functions for

rectangle, ellipse and triangle).

baSr baSe baSt 5.0

double s_rect(double a, double b);double s_ell (double a, double b); double s_tri (double a, double b);

• Header file for library areas2D.h

Page 31: The 5 th  lecture Jiří Šebesta

Example (2/2)

baSr

baSe

baSt 5.0

#include "areas2D.h"

double s_rect(double a, double b){ return a*b;}

double s_ell (double a, double b){ return 3.1415*a*b;}

double s_tri (double a, double b)

{ return 0.5*a*b;}

• Library functions in areas2D.cpp

Source code: Ex58.c + areas2D.c(h)

Page 32: The 5 th  lecture Jiří Šebesta

TOPIC OF THE NEXT LECTURE

Advanced data types

THANK YOU FOR YOUR ATTENTION