Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.

Similar presentations


Presentation on theme: "By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please."— Presentation transcript:

1 by Senem Kumova Metin 1 DATA TYPES

2 by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please type Y for YES / N for NO”); scanf(“…….”, &x);

3 by Senem Kumova Metin 3 WHAT IS THE APPROPRIATE TYPE ? ….. x; printf(“Please make your choise”); printf(“1 Addition\n”); printf(“2 Subtraction\n”); printf(“3 Division\n”); scanf(“…..”, &x); …..

4 by Senem Kumova Metin 4 WHAT IS THE APPROPRIATE TYPE ? ….. x; …… y; …… result; printf(“Please give two numbers to divide”); scanf(“…..”, &x); scanf(“…..”,&y); result= ……. x/y; printf(“Result of division is …”, result);

5 by Senem Kumova Metin 5 WHAT IS THE APPROPRIATE TYPE ? …… …….. printf(“Please write your name and surname”); scanf(“…..”, name); scanf(“…..”, surname); printf(“your name is …..”, name); printf(“your surname is …..”,surname);

6 by Senem Kumova Metin 6 FLOW OF CONTROL IF STATEMENT SWITCH STATEMENT LOOPS

7 by Senem Kumova Metin 7 FILL IN THE BLANKS FILL IN THE BLANKS int x; int y; printf(“Please enter 2 numbers to compare”); scanf(“%d”,&x); scanf(“%d”,&y); ……. ……

8 by Senem Kumova Metin 8 FILL IN THE BLANKS FILL IN THE BLANKS int x; printf(“Please make your choise”); printf(“1 Addition\n”); printf(“2 Subtraction\n”); printf(“3 Division\n”); scanf(“ %d”, &x); …….

9 by Senem Kumova Metin 9 FILL IN THE BLANKS FILL IN THE BLANKS int x; int result =0; ……. printf(“Please enter 10 numbers to sum”); ………. printf(“Result is %d”,result);

10 by Senem Kumova Metin 10 FUNCTIONS REVIEW

11 by Senem Kumova Metin 11 return_type function_name (input_parameter_list) { declarations/* body of the function */ statements return …… } return_type : * Zero or one data type ( int, char, float etc.) * “void” keyword can be used if there is no return value * Write the type of the return variable… input_parameter_list : * Zero or more variables (as int x, float c etc.) * If more than one input parameter exist, then put commas between them * If there is no parameter, “void” keyword can be usedFUNCTIONS

12 by Senem Kumova Metin 12 FUNCTION :EXAMPLE1 double twice (double x) /*header=declaration*/ { /* body starts here */ return x*2;} /* double twice (double x) output input parameter parameters this function is called “twice()”, it will return a data of type double, it gets an input data of type double */

13 by Senem Kumova Metin 13 FUNCTION :EXAMPLE2 If there is no return value, use “void” keyword as an output parameter If there is no input parameter, use “void” as an input parameter or do not write anything void menu(void) { printf(“ A\n ”); printf(“ B\n ”); printf(“ C\n ”); } void menu() { printf(“ A\n ”); printf(“ B\n ”); printf(“ C\n ”); }

14 by Senem Kumova Metin 14 FUNCTION :EXAMPLE3 int add1( int x, int y) {return x+y;} int add2( int x, int y) {int result; // local variables can be // declared in functions result =x+y; return result;}

15 by Senem Kumova Metin 15 FUNCTION :EXAMPLE4 int sum( int a, int b) { /* If-else, for, while, do while etc. statements can be used in functions */ int i; int s=0; for (i=a;i<b;i++) s=s+i; return s; }

16 by Senem Kumova Metin 16 Return statement The return statement may or may not include an expression The return statement terminates the execution of the function, and closes memory space opened for all local variables (kills all local variables of function) EXAMPLE: float f(int a, char b) { int i; ….. return i; /* value of i will be converted to float */ /* return (i); */ /* return value can also be written in braces */ }

17 by Senem Kumova Metin 17 return statement : EXAMPLES char func1( int a, int b) { return a+b; } double my_sqrt( int a ) { return sqrt(a); } int my_max1( int y, int z) { return y>z? y:z ; } int my_max2 (int y, int z, int t, int x) { int a, b; a= my_max1(y,z); b=my_max1(t,x); return my_max1(a,b);} 1 2 3 4 Functions can call other functions

18 by Senem Kumova Metin 18 FUNCTION PROTOTYPES Each function has to be declared before it is used There are two ways to achieve this rule.. –Define the functions before used –Write the function prototype of the function before used ( Just declaration not definition) Function prototype includes header information : –Return type –Function name –Input parameter list

19 by Senem Kumova Metin 19 FUNCTION PROTOTYPES : EXAMPLES char func1( int a, int b, int c); double func2( char a ); int func3( int y[], int z); int funct4(int y[], char z[], int t, int x); 1 2 3 4

20 by Senem Kumova Metin 20 Function Definition Order EXAMPLE 1: #include int func_2(); int func_1(); int func_3(); void main(void) { func_1(); func_2(); func_3(); } int func_2(){…}; int func_1(){…}; int func_3(){…}; EXAMPLE 2: #include int func_2(); int func_1() { func_3(); } int func_3(); void main(void) { func_2(); func_1(); } int func_2(){…}; int func_3(){…};

21 by Senem Kumova Metin 21 CALL BY VALUE If a variable is passed to a function, the stored value in the calling environment will not be changed!!! void my_increment (int n) { n=n+1; } void main(void) {int n=9; printf(“%d\n”,n); my_increment(n); // call function my_increment() // by value printf(“%d\n”,n); }

22 by Senem Kumova Metin 22 CALL BY REFERENCE If a variable’s address is passed to a function, the stored value in the calling environment may be changed!!! void my_increment (int * n) { *n=*n+1; } void main(void) {int n=9; printf(“%d\n”,n); my_increment(&n); // call function my_increment() // by value printf(“%d\n”,n); }

23 by Senem Kumova Metin 23 RECURSION An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task If a function calls itself, then it is called recursive !!! There are two parts to the definition of a recursive solution: –Base case: Simple, non-recursive solution –General method: Uses simpler version of the problem to solve harder problem

24 by Senem Kumova Metin 24 RECURSION EXAMPLE 1 // ITERATION int sum(int N) { int i; int SUM =0; for (i=1; i<=N; i++) SUM=SUM+i; return SUM; } // RECURSION int sum(int N) { if(N==1) return 1; else return(N+sum(N-1));} /* BASE CASE : N==1  1 METHOD : sum(N) = N + sum(N-1) if BASE CASE else METHOD */

25 by Senem Kumova Metin 25 RECURSION EXAMPLE 2 BASE CASE  N=1 return 1 METHOD  sum(N)= sum(N-1)+1/N double sum(int N) {if(N==1) return 1; else return (sum(N-1)+1/N); }

26 by Senem Kumova Metin 26 RECURSION vs ITERATION Iteration in computing is the repetition of a process within a computer program Recursion is a method of defining functions in which the function being defined is applied within its own definition

27 by Senem Kumova Metin 27 EXAMPLE: RECURSION & ITERATION Write a function that prints numbers from 1 to N using iteration Convert your function to work in a recursive way void main( ) { print(5); } // 1 2 3 4 5

28 by Senem Kumova Metin 28 /* ITERATION */ void print( int N) { int i; for (i=1;i<=N;i++) printf(“%d\t”,i); } /* RECURSION*/ void print( int N) { if(N==1) printf(“1\t”); else {print(N-1); printf(“%d\t”,N); }

29 by Senem Kumova Metin 29 FACTORIAL EXAMPLE: RECURSION & ITERATION /*iterative version n! = 1*2*3* … * n = n*(n-1)*(n-2) * …*3*2*1 */ int factorial (int n) { int product = 1; for(;n>1;--n) product = product*n; return product;} /* recursive version n! = n * (n-1)!*/ int factorial(int n) { if(n<=1) return 1; else return ( n*factorial(n-1)); } /* BASE CASE : n<=1  1 METHOD : n!=n*(n-1)! */ factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) )) = 3 * ( 2 * ( 1 * 1 ) )) = 6


Download ppt "By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please."

Similar presentations


Ads by Google