by Dinesh Thakur Category: Function and Pointer

Passing Arguments by Value

The arguments to a function may be passed on by value or by pointers. In the first case the copies of values of parameters are passed on to the function. The function can only manipulate these copies. So, the original values of parameters are not affected. Their values cannot be changed by the function because function does not know where the parameters are stored in the memory. For example, if the function simply swapped the values of its parameters, it is only swapping the copies of the values of parameters. The original values of parameters are not affected. In Program, the arguments are passed on to the function by values. These values are changed by the function and then an expression is evaluated. The function may be called any numbers of times, the output of the program as well as the data fed to function do not change. This is the benefit of passing the arguments by value.

However, when the arguments are passed on by pointers, the actual parameters are passed on to the function. The function gets the values from the actual addresses of the parameters. Any change in the values of arguments by the function gets stored at the addresses of parameters. Thus, the parameters values also get changed. Every time the function is called the output is different as well as the original data is also changed. This is illustrated in Program

Illustrates passing arguments by value

#include<stdio.h>
int Sum(int a, int b) // function definition
{
     a    *=2;
  b *=4;
  return a+b;
}
int main ()
{
  int A,B, Result;
  clrscr();
  A = 2;
  B = 3;
  printf("Before calling function, ");
  printf("A = %d\t B = %d\n", A, B);
  Result= Sum(A,B); //first call of function
  printf("After the first function call.\n");
  printf("Result= %d\tA = %d\tB = %d\n", Result, A, B);
  Result= Sum(A,B); // second call of function
  printf("After the second function call.\n");
  printf("Result= %d\tA = %d\tB = %d\n", Result, A, B);
  return 0;
}
The expected output is given below.

  Passing arguments by value and by Pointers in C

The values of A,B and and Result do not change even if the function is called repeatedly. This is because the function gets only copies of the values of parameters; it may change the copies but cannot change the variables because the function does not know where they are stored.

Passing Arguments by Pointers

Pointers keep the actual addresses of variables; therefore, passing arguments through pointers is as good as passing on the variables to the function. Program illustrates this fact.

#include<stdio.h>
int Sum( int *a, int *b) //function definition in pointers
{
  *a *=2;
  *b *=4;
  return *a + *b;
}
int main()
{
  int A,B , Result;
  clrscr();
  A= 2;
  B= 3;
  printf("Before calling the function, ");
  printf("A = %d\t B = %d\n",A,B);
  Result=Sum(&A, &B); /*function call with addresses of A and B*/
  printf("After the first function call.\n");
  printf("Result= %d\tA = %d\tB = %d\n", Result, A, B);
  Result= Sum(&A,&B); //second call of function
  printf("After the second function call.\n");
  printf("Result= %d\tA = %d\tB = %d\n", Result, A, B);
  return 0;
}

The output is as given below.

Passing arguments by value and by Pointers in C



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles