Upload
dominique-waterson
View
227
Download
2
Embed Size (px)
Citation preview
by Senem Kumova Metin 2
DATA TYPE?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);
by Senem Kumova Metin 3
WHAT IS THE APPROPRIATE TYPE ?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);…..
by Senem Kumova Metin 4
WHAT IS THE APPROPRIATE TYPE ?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);
by Senem Kumova Metin 5
WHAT IS THE APPROPRIATE TYPE ?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);
by Senem Kumova Metin 6
FLOW OF FLOW OF CONTROLCONTROL
IF STATEMENTIF STATEMENTSWITCH STATEMENTSWITCH STATEMENT
LOOPSLOOPS
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);…….……
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);…….…….
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);
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 used
FUNCTIONSFUNCTIONS
by Senem Kumova Metin 12
FUNCTION :EXAMPLE1FUNCTION :EXAMPLE1double 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
*/
by Senem Kumova Metin 13
FUNCTIONFUNCTION :EXAMPLE2: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 ”);
}
by Senem Kumova Metin 14
FUNCTION :EXAMPLE3FUNCTION :EXAMPLE3
int add1( int x, int y) { return x+y; }
int add2( int x, int y) { int result;
// local variables can be // declared in functionsresult =x+y;return result; }
by Senem Kumova Metin 15
FUNCTION :EXAMPLE4FUNCTION :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;}
by Senem Kumova Metin 16
Return statementReturn 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 */}
by Senem Kumova Metin 17
return statement : return statement : EXAMPLES 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
by Senem Kumova Metin 18
FUNCTION PROTOTYPESFUNCTION 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
by Senem Kumova Metin 19
FUNCTION PROTOTYPES : FUNCTION PROTOTYPES : EXAMPLES 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
by Senem Kumova Metin 20
Function Definition OrderFunction Definition OrderEXAMPLE 1:
#include<stdio.h>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<stdio.h>int func_2();
int func_1() { func_3(); }
int func_3();
void main(void){ func_2(); func_1(); }
int func_2(){…};int func_3(){…};
by Senem Kumova Metin 21
CALL BY VALUECALL BY VALUEIf 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);
}
by Senem Kumova Metin 22
CALL BY REFERENCECALL BY REFERENCEIf 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);
}
by Senem Kumova Metin 23
RECURSIONRECURSION• 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
by Senem Kumova Metin 24
RECURSION EXAMPLE 1RECURSION EXAMPLE 1
// ITERATIONint sum(int N) { int i;
int SUM =0;
for (i=1; i<=N; i++)
SUM=SUM+i;
return SUM; }
// RECURSIONint sum(int N){ if(N==1) return 1;
else return(N+sum(N-1)); }
/* BASE CASE : N==1 1METHOD : sum(N) = N + sum(N-1)
if BASE CASEelse METHOD
*/
)()1(.......43211
NsumiNNNi
i
by Senem Kumova Metin 25
RECURSION EXAMPLE 2RECURSION EXAMPLE 2
BASE CASE N=1 return 1METHOD sum(N)= sum(N-1)+1/N
double sum(int N){ if(N==1) return 1;
else return (sum(N-1)+1/N); }
)(11
1
1.......
4
1
3
1
2
1
1
1
1
NsumiNN
Ni
i
by Senem Kumova Metin 26
RECURSION vs ITERATIONRECURSION 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
by Senem Kumova Metin 27
EXAMPLE: EXAMPLE: RECURSION & ITERATIONRECURSION & 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
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);}
}
by Senem Kumova Metin 29
FACTORIAL EXAMPLE: FACTORIAL EXAMPLE: RECURSION & ITERATIONRECURSION & 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