## Types of function

### Function call type

1. call by value – When we pass actual value of an argument into formal argument of the function is called,  call by value. In this type of function call  any changes in formal argument inside called function does not effect the actual argument.
2. call by reference – When we pass address of an argument into formal argument of the function is called, call by reference. The address is used to access actual argument inside called function. In this case changes made to argument effect the actual argument.

Example of  call by value

 #include void gt_func2(int x, int y); /* Function prototype */ int main() {     int num1 = 10;     int num2 = 40;     gt_func2(num1, num2); /* Function call */     printf(“num1 = %d\n”,num1);     printf(“num2 = %d\n”,num2);     return 0; } void gt_func2(int x, int y) /* Function definition */ {     x = 20;     y = 50;     printf(“x = %d\n”,x);     printf(“y = %d\n”,y); } Output of this program is :- \$ ./a.out x = 20 y = 50 num1 = 10 num2 = 40 Explanation :- We can see in above C program  num1, num2 is actual argument and  x , y is formal argument.  Changes in formal argument is not effecting actual argument.

Example of call by reference

 #include void gt_func2(int *x, int *y); /* Function prototype */ int main() {     int num1 = 10;     int num2 = 40;     gt_func2(&num1, &num2); /* Function call */     printf(“num1 = %d\n”,num1);     printf(“num2 = %d\n”,num2);     return 0; } void gt_func2(int *x, int *y) /* Function definition */ {     *x = 20;     *y = 50;     printf(“x = %d\n”,*x);     printf(“y = %d\n”,*y); } Output of this program is :- \$ ./a.out x = 20 y = 50 num1 = 20 num2 = 50 Explanation :- We can see in above C program  num1, num2 is actual argument and  x , y is formal argument. We are sending address of formal argument and receiving through pointer in called function. We will study about pointer in next chapter. In output we can see, changes in formal argument is effecting actual argument.

This site uses Akismet to reduce spam. Learn how your comment data is processed.