Function in C: Example
Here is an example of using function.
#include<stdio.h>
/* 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
swap(&x,&y);
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
- C programming language supports function to provide modularity to the software.
- 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.
- By using function, you can divide complex tasks into smaller manageable tasks and test them independently before using them together.
- 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.