1. 2015-11-20
1
Unit 4
Functions
• Set of instructions defined to perform
specified task.
• Reduces complexity.
• Provide modularity.
• Easy for error detection and
debugging.
• Divide complex tasks into manageable
tasks.
Types of Functions
• Two types.
• Pre defined functions –
e.g. main(), strlen(), etc.,
• User defined functions –
e.g. add() – a sub-function
to perform addition.
Need & Advantages of Functions
• Program is too large and complex.
• Debugging, testing becomes difficult.
• Reduces length.
• Easy to detect and debug errors.
• Top-down approach.
• Avoids coding of repeated execution.
Three elements of Function
• Function declaration
• Function calling
• Function definition
• Declaration of a sub function in main
function – Function Declaration
• Calling of sub function in main function –
Function Calling
• Execution of main function – Function
Definition
• Return to the main function.
• Start executing the next statement after the
function calling statement.
• Syntax of function Declaration section
<<Return type>> funname(parameter list);
• Syntax of function Definition section
<<Return type>> funname(parameter list)
{
body of the function
}
• Syntax of function calling section
Funname(parameters);
2. 2015-11-20
2
Types of parameters
• Actual parameters - used during a function
call
• Formal parameters - used in function
definition and function declaration
Example function to display a value
#include<stdio.h>
void main()
{
void fun(int); //declaration
fun(10); //Call – Actual Parameters
}
void fun(int x) //definition – Formal Parameters
{
printf(“%d”,x);
}
Return statement
• Used when sub function returns some
integer value to the main function.
• Syntax:
•return(value);
Or
•return variable;
• Example:
•return(0); or return a;
Function prototypes
Depending upon arguments function is classified
into
•Function with no arguments and no
return value.
•Function with arguments and no return
value
•Function with arguments and with return
value.
•Function with no arguments and with
return value.
Function with no arguments and
no return value
#include<stdio.h>
void main()
{
void fun(); //no arguments
fun();
}
void fun()
{
int x=5;
printf(“%d”,x); //no return type
}
Function with arguments and no
return value
#include<stdio.h>
void main()
{
void fun(int); //integer argument
fun(10);
}
void fun(int x)
{
printf(“%d”,x); //no return value
}
3. 2015-11-20
3
Function with arguments and with
return value
#include<stdio.h>
void main()
{
int a;
int fun(int); //integer argument & integer return type
a=fun(10);
printf(“%d”,a); //a=11
}
int fun(int x)
{
x++;
return x; //return integer value
}
Function with no arguments and
with return value
#include<stdio.h>
void main()
{
int a;
int fun(); //no argument & integer return type
a=fun();
printf(“%d”,a); // a= 5
}
int fun()
{
int x=5;
return x; //return value
}
Parameter Passing Methods
Two ways of passing parameters to a function
are
• Call by value
• Copies the values of actual parameters
into formal parameters and actual
parameters doesn’t change.
• Call by reference
• Copies the address of the actual
parameters into the formal parameters.
Call by Value
• Calling a function with parameters passed as
values
int a=10; void fun(int a)
fun(a); {
defn;
}
Here fun(a) is a call by value.
Any modification done with in the function is local to
it and will not be effected outside the function
Call by reference
• Calling a function by passing pointers as
parameters (address of variables is passed instead
of variables)
int a=1; void fun(int *x)
fun(&a); {
defn;
}
Any modification done to variable a will effect
outside the function also
Example program – Call by value
#include<stdio.h>
void main()
{
int a=10;
printf(“%d”,a); a=10
fun(a);
printf(“%d”,a); a=10
}
void fun(int x)
{
printf(“%d”,x) x=10
x++;
printf(“%d”,x); x=11
}
4. 2015-11-20
4
Explanation – Call by value
Example Program – Call by
reference
#include<stdio.h>
void main()
{
int a=10;
printf(“%d”,a); a=10
fun(&a);
printf(“%d”,a); a=10
}
void fun(int *x)
{
int t;
t=*x;
printf(“%d”,t) x=10
t++;
printf(“%d”,t); x=11
}
a and x are referring to same location. So value will be over
written.
Explanation – Call by reference Conclusion
• Call by value => copying value of variable
in another variable. So any change made
in the copy will not affect the original
location.
• Call by reference => Creating link for the
parameter to the original location. Since
the address is same, changes to the
parameter will refer to original location
and the value will be over written.
Recursion or Recursive Function
Definition: A function is called recursion or recursive function if it calls
by itself i.e. the statements inside the function has a call to the same
function.
e.g.
void main( )
{
void add( );
…..
add();
}
add( )
{
add( );
}
Function declaration
Function calling
Function definition
Advantages of Recursion
• Useful for branching processes
•Written with less number of statements.
•Requires only few variables which requires program clean
•Recursive functions are more effective in computing values
Disadvantages of Recursion
•Hard to think logic of the function.
•Difficult to debug the code containing recursion.
5. 2015-11-20
5
Applications of Recursive
function
•Factorial of a number
•Finding length of the string
•Fibonacci series generation
•Towers of hanoi program
Factorial - Recursion
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,b;
int fact(int);
clrscr();
printf(“nEnter the value to compute:”);
scanf(“%d”,&n);
b=fact(n);
printf(“nFactorial of %d is %d”,n,b);
getch();
}
int fact(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*fact(n-1);
}
Fibonacci - Recursion
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,n;
int fibo(int);
clrscr( );
printf(“nEnter no. of elements in series:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“%d”,fibo(i));
}
getch( );
}
void fibo(int i)
{
if(i==0)
{
return 0;
}
if(i==1)
{
return 1;
}
return fibo(i-1)+fibo(n-2);
}
Towers of Hanoi - Recursion
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int disk, moves;
void hanoi(int,char,char,char);
clrscr( );
printf(“nEnter no. of disks:”);
scanf(“%d”,&disk);
moves=pow(2,disk)-1;
printf(“nNo.of moved req:%d”,moves);
hanoi(disk, ‘A’, ‘C’, ‘B’);
getch( );
}
void hanoi(int x,char from, char to, char aux)
{
if(x==1)
printf(“nMove disk from %c to %c”,from,to);
else
{
hanoi(x-1,from,aux,to);
printf(“nMove disk from %c to %c”,from,to);
hanoi(x-1,aux,to,from);
}
}
• Pointer
• Definition
• Initialization
• Pointers arithmetic
• Pointers and arrays
• Eg programs
Pointers
Definition:
Pointer is a variable that holds the address of variable or
function.
It is declared same as other variable but the variable
preceded by *(asterisk) (i.e) indirection operator
Syntax: datatype *variable_name;
eg: int *a,*c; char *b;
Here a stores address of integer variable and b stores
address of character variable.
It is a variable whose value is the address of another
variable(same data type).
It is also called address variable and it is a derived data type
Pointers can be used to access and manipulate data stored
in the memory
6. 2015-11-20
6
Features of Pointers
• More effective and useful in handling
arrays.
• Used to return multiple values from a
function.
• Supports dynamic memory management.
• Reduces complexity and length of a
program.
• Reduces execution time of a program.
Limitations of Pointers
• Should have prefix “*”
• Combination of data types not allowed
Accessing the address of a variable:
Address for the variable is given by
machine
The symbol &(address of operator) is used
to get the address of the variable.
Syntax: &variable_name;#include<stdio.h>
#include<conio.h>
void main()
{
int *a , b=10;
a=&b;
clrscr();
printf("n Address of variable b is
%u",a);
//printf("n Address of variable b is
%u",&b);
printf("n value of b is %d ",*a);
getch();
}
10
b
655
24
655
24
655
24
avaria
ble
655
22
655
22
value
addr
ess
&b=65524
&a= 65522
Value of a=
65524
Value of b=10
a=&b; a=addr of
b(65524)
*a value of a
(65524) 10
Initialization of variable:
• The process of assigning the address of a variable to a
pointer variable is known as initialization.
• Eg: p=&n; p contains address of variable n, the data
item of n can be accessed by *p
Initialize pointer variable while declaring it:
int n=15;
int *p=&n; initialization while declaring
#include<stdio.h>
#include<conio.h>
void main()
{
int n=15;
int *p;
p=&n;
clrscr();
printf("%d",*p);
getch();
}
15
#include<stdio.h>
#include<conio.h>
void main()
{
int n=15;
int *p=&n;
clrscr();
printf("%d",*p);
getch();
}
15
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int *c,*d;
clrscr();
printf("nEnter the values of a and b:");
scanf("%d%d",&a,&b);
c=&a;
d=&b;
printf("nValue of a is:%d",a);
printf("nValue of a is:%d",*c);
printf("nAddress of a is:%u",&a);
printf("nAddress of a is:%u",c);
printf("nValue of b is:%d",b);
printf("nValue of b is:%d",*d);
printf("nAddress of b is:%u",&b);
printf("nAddress of b is:%u",d);
printf("nAddress of pointer variable c
is:%u",&c);
printf("nAddress of pointer variable d
is:%u",&d);
getch();
}
Double pointer
Definition: is a variable used to store address of another
pointer variable.
Syntax:
data_type **var_name;
e.g.
void main()
{
int c;
int *b,**a;
b=&c; //b displays address of c and *b displays values of c
a=&b; //a->double pointer variable and displays address of b
and *a displays address of c
}
Double pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int n=15;
int *p=&n;
int **a=&p;
clrscr() ;
printf("n *p is %u",*p);
printf("n **a ia %u",**a);
getch();
}