Академический Документы
Профессиональный Документы
Культура Документы
What is a Function?
A function is a block of code that performs a
particular task.
A function can be called from the same or
another function whenever required. Hence we
can avoid rewriting the same code. It also
improves the readability of the program.
Function may take some input in the form of
parameters and sent out some output in the form
of return value.
primef1.c
printprime()
{
int j,i,flag=1;
j=num/2;
for (i=2;i<=j;i++){
if(num%i==0){
flag=0;
printf("Not prime");
break;
}
}
if (flag) printf("Prime number");
}
4
Return value
Some compiler throws a warnings if function does
declared as void.
Function declaration
return_type function_name(argument-list)
Where argument-list is
Data-type var-name1, Data-type varname2, Data-type var-nameN
If return_type is not specified it is assumed to be
an int.
argument-list can contain 0 one or more
arguments.
function_name must be unique. It should not
clash with any variable name or other function name.
Variables declared inside the function and the
argument-list variables are local to the function. They
cannot be accessed outside the function.
6
return
The return statement is used to return a value from a
function.
When return is encountered the control go back to the calling
function.
Example:
return (a);
return (15);
return 0;
return;
Prototyping
Complier assumes that any C function would
return a type int.
Any function that returns types other than int
must inform the compiler by what is called
prototyping.
Prototyping is declaring the function that needs
to be called within the calling function.
Note that declaration of a function is different
from definition of a function.
void pprime()
{
int j,i,flag=1;
j=num/2;
for (i=2;i<=j;i++){
if(num%i==0){
flag=0;
printf("Not prime");
break;
}
}
if (flag) printf("Prime number");
return;
optional
}
10
11
gross.c
12
13
static variable
Solution to this is to declare the count variable
as static variable.
Static variables are automatically initialized the
first time to 0.
It retains the value between the function calls
16
me 1 times
me 2 times
me 3 times
, count);
17
static local:
default:0
18
Call by value
void main(){
void f(int i,int j);
int i=0, j=0;
printf("Before calling f(): value of i=
%d and value of j=%d\n", i,j);
f(i,j);
printf("After calling f(): value of i=
%d and value of j=%d\n", i,j);
}
19
Observation
What do you observe?
The parameter values changed in the called
function code are not reflected in the calling
function.
This is because the parameters are passed by
value to the function.
Both functions have their own copy of the
variables. Only the values of the variables of the
calling function get copied to the variables of the
called function
21
Address
main()
i
j
1000
1001
1002
Call f(0,0)
1003
i
j
1004
f()
i
j
i++;
j++;
i
j
0
0
0
1005
MEMORY
22
main()
i=0
1000
j=0
1001
Value of i and j
in 1000 and
1001 remain
the same!
i=1
Calls
j=1
f()
i=0
2000
j=0
2001
i++;
j++;
In memory
23
Call by reference
24
Introduction to pointers
A pointer is a variable that holds an address of a variable in
memory and using this pointer we can access the value stored
in that location.
Declaration:
Data-type *variable_name;
Data-type in the above syntax represents what type of data the
pointer points to.
Example:
double i=10;
double *p=&i;
26
double i=10;
double *p=&i;
printf(%lf,*p);
printf(%d,p);
Prints 10
Prints the address
27
point.c
Question ?
?
Call by reference
/* program : call by value demo */
void main(){
void f(int *i,int *j);
int i=0, j=0;
printf("Before calling f(): value of i=
%d and value of j=%d\n", i,j);
f(&i,&j);
printf("After calling f(): value of i=
%d and value of j=%d\n", i,j);
}
29
Recursion
A function that calls itself is a recursive function.
It is very important that a recursive function has an
exit point. Otherwise it will get into infinite loop .
int i;
main(){
f();}
int f(){
f();
}
Without termination
condition : infinite loop
int i;
main(){
f();}
int f(){
i++;
if(i>5) return ;
f();} Termination
condition
specified.
Executes
successfully.
31
33
Flow
Assume num=9
prime(2) 1 {if(num%i==0) return 0; 9%2!=0
i++;2++ 3
if(i>num/2) return 1;3>(9/2) No!
Return value: 0
else
4
prime(i); prime(3)
}
2
Return value: 0
Fibonacci numbers
Non-recursive
main(){
int f1=0,f2=1,f3,i;
printf("%d, %d,",
f1,f2);
for(i=0;i<8;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
printf("%d,", f2);
}
}
fib.c
Recursive
main(){
void fibo(int f1, int f2);
int f1=0,f2=1,f3,i;
printf("%d, %d,", f1,f2);
fibo(f1,f2);
}
void fibo(int f1, int f2){
static int count;
Termination condition
int f3;
if(count>8) return;
f3=f1+f2;
f1=f2; f2=f3;
printf("%d,", f2);
count++;
fibr.c
fibo(f1,f2);}
35
Use of recursion
Recursive programs sometime seem rather difficult in
Recursive code
main(){
int num, res;
printf("enter a number");
scanf("%d",&num);
res=fact(num);
printf("%d",res);
}
int fact(int n){
int f;
if(n==1) return 1;
f=n*fact(n-1);
return f;
}
37
Flow
num=3
fact(3)
Return 6
{int f;
if(n==1) return 1; n=3
Return 2
f=n*fact(n-1); 3*fact(2)
return f;}
{int f;
if(n==1) return 1; n=2
f=n*fact(n-1); 2*fact(1)
return f;}
{
int f;
if(n==1) return 1; n=1
f=n*fact(n-1);
return f;}
Return 1
38
Word of caution!
Recursive code may be difficult to understand
and debug. Hence it should be used only in
places where it makes the overall program
simpler.
Recursive code adds to overhead of multiple
function calls.
39