Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why functions segments of code that repeat several times

Similar presentations


Presentation on theme: "Why functions segments of code that repeat several times"— Presentation transcript:

1

2 Why functions segments of code that repeat several times
better readability abstraction of code smaller program size IAG0581

3 Structural programming
int main(void){ int a, b, c; do{ printf("Please enter A: "); scanf("%d", &a); if(a <= 0) printf("A must be positive!\n"); }while(a <= 0); printf("Please enter B: "): scanf("%d", &b); if(b <= 0) printf("B must be positive!\n"); }while(b <= 0); //... return 0; } IAG0581

4 Functional programming
int readPositiveInt(char *name){ int x; do{ printf("Please enter %s: ", name); scanf("%d", &x); if(x <= 0) printf("%s must be positive!\n", name); }while(x <= 0); return x; } int main(void){ int a, b, c; a = readPositiveInt("A"); b = readPositiveInt("B"); //... return 0; IAG0581

5 Declaring a function returntype functionName(parameters){ functionBody } parameters is just a list of variables separated by comma: int a, int b use "void" if no parameters IAG0581

6 Datatypes, simple types
char a single character or small number: int integer number float floating point number void signifies no type When used as parameters, these are unidirectional IAG0581

7 Datatypes, pointers char * pointer to a character or character array
int * pointer to integer or integer array float * pointer to float or floating point array void * pointer to memory without type specified When used as parameters, these are bidirectional IAG0581

8 Datatypes, arrays char[] a character array int[] an array of integers
float[] a array of floating point numbers When used as parameters, these are bidirectional IAG0581

9 Calling a function even if no parameters, ( ) are necessary
even if function returns something it can be called without assignment For example: int doSmth(void){ //... return 1; } int main(void){ doSmth(); return 0; IAG0581

10 C compiler single pass compiler
accepts only variables and functions that have been declared before they are used sometimes necessary to declare that a function exists before providing code for that function IAG0581

11 Function prototypes used to tell compiler that a function by that name exists and what parameter types it takes parameter names are unimportant For example: int readPositiveInt(char *); IAG0581

12 Function prototypes int readPositiveInt(char *); int main(void){ //... a = readPositiveInt("A"); } int readPositiveInt(char *name){ IAG0581

13 Variable scope variable exists in block where it is declared
outside block it doesn't exist variable can be redefined in an inner block each function is separate block IAG0581

14 Variable scope int fn1(void){ int x = 2; //... } int main(void){ int a, b, x; x = 5; a = fn1(); if(a < b){ float x = 3.0; x++; IAG0581

15 Function with no data exchange
void welcome(void){ printf("This program ..."); printf("Written by..."); } int main(void){ welcome(); ... IAG0581

16 Getting a result from a function
result is returned with return keyword. what's returned should be of same type as declared returntype, if not it will be converted IAG0581

17 Getting a result from a function
int test(void){ float f = 0.8; return f; // will return 0, cos converted to int } int test2(void){ ... return; //warning !! void test3(void){ int x = 10; return x; //warning !! IAG0581

18 Getting multiple results
only 1 item of returntype can be returned returntype could be complex type, like record or pointer etc. use pointers as parameters so function can access memory outside of its scope Example you should know: scanf("%d", &a); & operator returns address of where variable is located in memory IAG0581

19 Getting multiple results
int div(int a, int b, int *result, int *remainder){ if(b == 0) return 0; *result = a / b; *remainder = a % b; return 1; } int main(void){ //... int s, t; div(15, 6, &s, &t); IAG0581

20 Getting multiple results with arrays
arrays are always pointers to first element function can access all elements if it knows how many there are IAG0581

21 Getting multiple results with arrays
void doSmth(int n, float M[n]){ int i; for(i = 0; i < n; i++) M[i] = //... } int main(void){ int Arr[10]; doSmth(10, Arr); ... IAG0581

22 Conditions a function call can be put anywhere in code, also in place of conditions if you have complex condition then make it into a function, especially if needed more than once For example: while( a < b && a < c || a + b < c + 1 ){ //... } IAG0581

23 Conditions int isTrue(int a, int b, int c){ return a < b && a < c || a + b < c + 1; } int main(void){ //... if(!isTrue(3, b, x)) while(isTrue(3, b, x)){ IAG0581

24 Function as parameter permits you call a function that has been passed as a parameter to your code alternatively you can write a function that behaves as a parameter for some function you want to call typical example qsort void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); IAG0581

25 Function qsort() qsort() sorts an array if you supply it a function that can compare 2 elements in it, essentially a condition function int isLess(const void *aa, const void *bb){ int *a = (int*)aa; int *b = (int*)bb; return (*a) - (*b); } int main(void){ int M[10]; //... qsort(M, 10, sizeof(int), isLess); IAG0581

26 Function as parameter int readInt(char *name, int (*suits)(int), char *cond){ int x; do{ printf("Please enter %s: ", name); scanf("%d", &x); if(!suits(x)) printf("%s must be %s!\n", name, cond); }while(!suits(x)); return x; } int isPos(int a){ return a > 0; int main(void){ int a, b, c; a = readInt("A", isPos, "positive"); b = readInt("B", isPos, "positive"); //... IAG0581


Download ppt "Why functions segments of code that repeat several times"

Similar presentations


Ads by Google