C Programming language

Function in C: Example
Previous Home Next

Here is an example of using function.

  /* functions declaration */
  void swap(int *x, int *y);
 void main()
 int x = 10;
 int y = 20;
 printf("x,y before swapping\n");
 printf("x = %d\n",x);
 printf("y = %d\n",y);
  // using function swap
  printf("x,y after swapping\n");
 printf("x = %d\n",x);
 printf("y = %d\n",y); 
 }  [an error occurred while processing this directive]
  /* functions implementation */
  void swap(int *x, int *y){
 int temp = *x;
 *x = *y;
*y = temp;

Here is the output
x,y before swapping
x = 10
y = 20
x,y after swapping
x = 20
y = 10

Benefits of using function

  1. C programming language supports function to provide modularity to the software.
  2. One of major advantage of function is it can be executed as many time as necessary from different points in your program so it helps you avoid duplication of work.
  3. By using function, you can divide complex tasks into smaller manageable tasks and test them independently before using them together.
  4. In software development, function allows sharing responsibility between team members in software development team. The whole team can definea set of standard function interface and implement it effectively.
Previous Home Next