Function 2
Function 2
Contents :
#Definition
#Types :
I) Pre defined( in-built ) function
II) User-defined function
# Types ( Classsification )
#Property :
i) Calling method ( Data passing method ) :
i) call by value
ii) call by reference
ii) Global & local variable
iii) Recursion
Definition :
Or
Types:
ii) such as
<stdio.h> --> printf(), scanf() , putchar () , getchar() etc.
Iii) These functions are already defined in header files need to include only
in link section.
i) As the name itself indicating it is user program i.e. user has to create .
ii) syntax :
Where,
return _ type : Here , we declare the data type of the value returned by
functions. It may be char, int ,float or double type.
If no return then it should be void
Iii) Example :
ii) syntax
function-name( parameters );
iii) Example
add ( n1 , n2) ;
ii) example
}
Example :
#include<stdio.h>
void main( )
{
int n1 , n2 , a;
return ( m1+m2);
}
Types ( Claasification )
i) A Function can be claasified into four types based upon return type &
parameter
#include<stdio.h>
void main( )
{
int n;
return( 35);
}
O/P : I) first iteration
#include<stdio.h>
void main( )
{
prime( ) ; function call
}
int prime ( )
{
int a , m ;
return( 35);
}
O/P :
Enter the i/p for m
11
given number is prime
III) A function without return type & with parameter
#include<stdio.h>
void main( )
{
printf(“\n Enter the i/p for n \n”);
scanf(“%d”, &n); // i/p stmt
return( 35);
}
O/P :
Enter the i/p for n
17
given number is prime
IV) A function without return type & without parameter
#include<stdio.h>
void main( )
{
prime( ) ; function call
}
void prime ( )
{
int a , m ;
O/P :
Enter the i/p for n
29
given number is prime
Calling (Data passing) method
There are two methods by using which one can pass the data into function
I) call by the value
II) call by the reference ( address )
ii) Example
#include<stdio.h>
int main()
{
int n1 , n2 ; // declaration stmt
return (34) ;
}
void swap( int m1 ,int m2)
{
int t ;
printf(“Before swapping : %d %d”, m1 , m2 );
t = m1 ;
m1=m2;
m2= t ;
printf(“After swapping : %d %d”, m1 , m2 );
}
O/P :
Enter the i/p for n1 & n2
5 8
Before swapping : 5 8
After swapping : 8 5
Here , n1 & n2 are called actual parameters whereas m1 & m2 are called
formal ( dummy) parameters
II) call by the reference ( address )
ii) Example
#include<stdio.h>
int main()
{
int n1 , n2 ; // declaration stmt
return (34) ;
}
t = m1 ;
m1=m2;
m2= t ;
printf(“After swapping : %d %d”, *m1 , *m2 );
}
O/P :
Enter the i/p for n1 & n2
5 8
Before swapping : 5 8
After swapping : 8 5
Scope of variable
i) global variable
i) global variable
iii) Example
#include<stdio.h>
void main()
{
int m=45; // local variable
iii) Example
#include<stdio.h>
void main()
{
int m=45; // local variable
{
int m=5;
printf(“\n value of m=%d\n”, m );
}
printf(“\n value of m=%d\n”, m );
}
O/P :
value of m= 5
value of m=45