FUNCTION

 A function is a set of statements that take inputs, do some specific computation and produces output.


The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.

•Types of function :

Depending on whether a function is defined by the user or already included in C compilers, there are two types of functions in C programming

  • Library Functions : functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  • User Defined Functions : functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.


Advantage of functions in C :

  • By using functions, we can avoid rewriting same logic/code again and again in a program.
  • We can call C functions any number of times in a program and from any place in a program.
  • We can track a large C program easily when it is divided into multiple functions.
  • Reusability is the main achievement of C functions.
  • However, Function calling is always a overhead in a C program.
1.ADDITION PROGRAMME USING FUNCTION:

#include<stdio.h>
void add()
{
int x,y=70,z=50;
x=y+z;
printf("Add=%d",x);
}
int main()
{
add();
}

•Output 

Add=120

      2.SUBTRACTION PROGRAMME USING                                 FUNCTION:

#include<stdio.h>
void sub()
{
int x,y=70,z=50;
x=y-z;
printf("Sub=%d",x);
}
int main()
{
sub();
}

•Output 

Sub=20

    3.MULTIPLICATION PROGRAMME USING                                 FUNCTION:

#include<stdio.h>
void multi()
{
int x,y=70,z=50;
x=y*z;
printf("Multi=%d",x);
}
int main()
{
multi();
}

•Output 

Multi=3500

4.DIVISION PROGRAMME USING FUNCTION:

#include<stdio.h>
void div()
{
int x,y=40,z=5;
x=y/z;
printf("Div=%d",x);
}
int main()
{
div();
}

•Output 

Div=8

     5.RECTANGLE AREA USING FUNCTION: 

#include<stdio.h>
void area(int height,int width)
{
int ar=height*width;
printf("Area of rectangle=%d",ar);
}
int main()
{
int h,w;
printf("Enter height=");
scanf("%d",&h);
printf("Enter width=");
scanf("%d",&w);
    area(h,w);
}

•Output 

Enter height 
20
Enter width 
30
Area of rectangle =600

        6.CIRCLE AREA USING FUNCTION:

#include<stdio.h>
void area(float radius)
{
float ar=3.14*radius*radius;
printf("Area of circle=%f",ar);
}
int main()
{
float r;
printf("Enter radius of circle=");
scanf("%f",&r);
    area(r);
}

•Output 

Enter radius of Circle 
2.2
Area of Circle= 15.197

  7.FUNCTION WITH NO RETURN TYPE AND                        NO PARAMETER:

#include<stdio.h>
void add()
{
int x,y=70,z=50;
x=y+z;
printf("Add=%d",x);
}
int main()
{
add();
}

•Output 

Add=120

   8.FUNCTION WITH NO RETURN TYPE AND                         WITH PARAMETER:

#include<stdio.h>
int add(int y,int z)
{
int x;
x=y+z;
return x;
}
int main()
{
printf("Add=%d",add(10,20));
}

•Output 

Add=30

     9.FUNCTION WITH  RETURN TYPE AND                         WITH NO PARAMETER:

#include<stdio.h>
int add()
{
int x,y=70,z=50;
x=y+z;
return x;
}
int main()
{
printf("Add=%d",add());
}

•Output 

Add=120

   10.FUNCTION WITH  RETURN TYPE AND                         WITH PARAMETER:

#include<stdio.h>
int add(int y,int z)
{
int x;
x=y+z;
return x;
}
int main()
{
printf("Add=%d",add(10,20));
}

•Output 

Add=30

                   11.CALL BY VALUE: 

#include<stdio.h>
void add(int y,int z)
{
int x;
x=y+z;
printf("Add=%d",x);
}
int main()
{
add(10,20);
}

•OUTPUT 

Add=30

               12.CALL BY REFERENCE: 

#include<stdio.h>
int add(int *y,int *z)
{
int x;
x=*y+*z;
printf("Add=%d",x);
}
int main()
{
int a=20,b=50;
add(&a,&b);
}

•OUTPUT 

Add=70

  13.FUNCTION WITH DEFAULT PARAMETER :

#include<stdio.h>
int add(int y=10,int z=20)
{
int x;
x=y+z;
    printf("Add=%d\n",x);
}
int main()
{
printf("Without Parameter\n");
add();
printf("With Parameter\n");
add(40,60);
}

•Output 

Without parameter 
Add=30
With parameter 
Add=100

       14.PASSING ARRAY TO FUNCTION: 

#include<stdio.h>
int array(int arr[5])
{
int sum=0;
printf("Element is given below\n");
for(int i=0;i<=4;i++)
{
printf("%d ",arr[i]);
sum=sum+arr[i];
}
printf("\nTotal Sum=%d",sum);
}
int main()
{
int b[5];
printf("ENter 5 integer value one by one\n");
for(int j=0;j<=4;j++)
scanf("%d",&b[j]);
array(b);
}

•OUTPUT 

Enter 5 integer value one by one 
10
50
60
70
90
Element is given below 
10 50 60 70 90
Total sum =280

 15.RECURSION PROGRAMME IN FUNCTION:

#include<stdio.h>
int table(int no)
{
  if(no!=11)
  {
  printf("%d\n",no);
  no++;
  table(no);
  }
}
int main()
{
table(1);
}

•Output 

1
2
3
4
5

Comments

Popular posts from this blog

Java vs C++

STRUCTURE

SWITCH PROGRAMME