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.

Syntax:


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

Function Example


#include
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


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

							

Function with no arguments and a return value


#include
#include
void main()
{
float sum;
float total();
clrscr();
sum = total();
printf(" Sum = %f\n" , sum);
}
float total()
{
float a, b;
a = 5.0 ;
b = 15.0 ;
return(a+b);
}						
							

Function with arguments and no return value


#include
#include						
   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);        
        printline('Z');                                        
        value(principal,inrate,period);                        
        printline('C');                                        
   }                                                           

  void printline(char ch)                                               
   {                                                           
        int i ;                                                
        for(i=1; i <= 52; i++)                                 
             printf("%c",ch);                                  
        printf("\n");                                          
   }                                                           
   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;                                    
        }                                                      
        printf("%f\t%f\t%d\t%f\n",p,r,n,sum);                  
   }
	
							

Function with arguments and a return value


#include
float calculate_area(int);
int main()
{
    int radius;
    float area;
    printf("\nEnter the radius of the circle : ");
    scanf("%d",&radius);
    area = calculate_area(radius);
    printf("\nArea of Circle : %f ",area);
    return(0);
}
float calculate_area(int radius)
{
    float areaOfCircle;
    areaOfCircle = 3.14 * radius * radius;
    return(areaOfCircle);
}							
							
							
Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!