Functions in C

A function is a block of code that performs a specific task.C allows programmer to define functions according to their need. These functions are known as user-defined functions. The following simple example of function.


return_data_type function_name(data_type parameter, ......);						

Function Example

int add(int a, int b);         
int main()
    int n1,n2,sum;
    printf("Enters two numbers: ");
    scanf("%d %d",&n1,&n2);
    sum = add(n1,n2);        
    printf("sum = %d",sum);
    return 0;
int add(int a,int b)         
    int r;
    r = a+b;
    return r;

There are four type of UDF

01 Function with no arguments and no return value

02 Function with no arguments and a return value

03 Function with arguments and no return value

04 Function with arguments and a return value

Example of all Types of UDF

Function with no arguments and no return value

void area(); 
void main()
void area()
    float area_circle;
    float rad;
    printf("\nEnter the radius : ");
    area_circle = 3.14 * rad * rad ;
    printf("Area of Circle = %f",area_circle);


Function with no arguments and a return value

void main()
float sum;
float total();
sum = total();
printf(" Sum = %f\n" , sum);
float total()
float a, b;
a = 5.0 ;
b = 15.0 ;

Function with arguments and no return value

   void printline (char c);
   voidvalue (float, float, int);                                                                
   main( )                                                     
        float principal, inrate;                               
        int period;                                            
        printf("Enter principal amount, interest");            
        printf(" rate, and period \n");                        
        scanf("%f %f %d",&principal, &inrate, &period);        

  void printline(char ch)                                               
        int i ;                                                
        for(i=1; i <= 52; i++)                                 
   voidvalue(float p, float r, int n)
        int year ;                                             
        float sum ;                                            
        sum = p ;                                              
        year = 1;                                              
        while(year <= n)                                       
            sum = sum * (1+r);                                 
            year = year +1;                                    

Function with arguments and a return value

float calculate_area(int);
int main()
    int radius;
    float area;
    printf("\nEnter the radius of the circle : ");
    area = calculate_area(radius);
    printf("\nArea of Circle : %f ",area);
float calculate_area(int radius)
    float areaOfCircle;
    areaOfCircle = 3.14 * radius * radius;
Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit