Tuesday 1 October 2013

Functions in C

Pointers                                                                                                                        Array


Functions:- it is used for performing specific task. mainly it is used for reusability.
C contains two types of function.

  • Library function.
  • User defined function

Library Function:-Built in function of C like main() , printf() , scanf() .these are the library function.

User defined Function:-the function which are made by the user known as the user defined function.
Basic steps for making the function in C.
1)Declaration
2)Calling
3)Definition.

Ex:-

#include<stdio.h>
#include<conio.h>
void print();   // declaration
void main()
{
printf("from main method");
print();    // calling
printf("main method end");
}
void print()  // definition
{
pritnf("print method called");
}


output:-
from main method
print method called
main method end

in the above Example we declared the return type of function as void which means function is not returning any value.

Types of User defined Function:-

  • Function with no argument and no return value
  • Function with arguments and no return value
  • Function with argument and return value.

Function with no arguments and no return value:-

Ex:-
#include<stdio.h>
#include<conio.h>
void print();   
void main()
{
printf("from main method");
print();    
printf("main method end");
}
void print()  
{
pritnf("print method called");
}

Function with  arguments and no return value:-

EX:-
#include<stdio.h>
#include<conio.h>
void sum(int a ,int b);   
void main()
{
printf("from main method");
print(2,3);    
printf("main method end");
}
void print(int a , int b)  
{
int c;
c=a+b
pritnf("%d",c);
}

Output:-
from main method
5
main method end

in the above example we pass the parameter to the function. the value 2  3 is passed into the a and b.

Function with  arguments and  return value:-


EX:-
#include<stdio.h>
#include<conio.h>
int sum(int a ,int b);   
void main()
{
int z;
printf("from main method");
z=print(2,3);
printf("%d",z);   
printf("main method end");
}
int print(int a , int b)  
{
int c;
c=a+b
return c;
}

output:-
from main method
5
main method end

in the above example  we use the return type  int in place of void. which means function will return int type value.
in the definition block we return C which contains the sum of a and b
and the we store the return value to z.


Recursion:-the function which calls itself is known as recursion.

Ex:-
#include<stdio.h>
int fact(int num);
main()
{
int n,factorial;
printf("enter the number");
scanf("%d",&n);
factorial=fact(n);
printf("%d",factorial);
}
int fact(int num)
{
if(num!=1)
return num*fact(num-1);  //recursion
}

Output  num =2
factorial =2

in the above example we find the factorial of the number using the recursion.
in the return statement we call function again.so the condition of if is true it calls the function whenever the conditions will false it return the program and cursor move to the main from where we called the function.






No comments :

Post a Comment