Code Scrappers

Function Arguments

If a function is supposed to use arguments, it must declare variables that accept the values of the arguments. These variables are referred to as the formal parameters of the function. The formal parameters performs like the other local variables inside the function and are created upon entry into the function and destroyed upon exit. While calling a function, the two ways through which arguments can be passed to a function are as follows:

Function call by value

The call by value method copies the actual value of an argument into the formal parameter of the function. In this method, changes made to the parameter inside the function have no effect on the argument.

C programming language uses call by value method to pass arguments by default. In a generalized way,it can be understood that code within a function cannot alter the arguments used to call the function.Consider the function swap() definition as follows.



void swap(int p, int q)

{

  int t;

  t = p;

  p = q;

  q = t;

  return;

}

Now, let's call the function swap() by passing actual values in the below example:


  #include <stdio.h>

  void swap(int p, int q);

  void main ()

{

  int a = 50;

  int b = 100;

  printf("Before swap-->\n");

  printf("a : %d\n", a );

  printf("b : %d\n", b );

  swap (a, b);

  printf("After swap-->\n");

  printf("a : %d\n", a );

  printf("b : %d\n", b );

}

Let's combine the above code in a single C file, compile and execute it, it will produce the following result:
Before swap-->
a :50
b :100
After swap-->
a :50
b :100
It can be observed that there is no change in the values though they had been changed inside the function.

Function call by Reference

The call by reference method copies the address of an argument into the formal parameter. Inside the defined function, the address is used to access the actual argument used in the call. This concludes that the changes made to the parameter affect the passed argument.

To pass value by reference, argument pointers are passed to the functions just like any other value. So,subsequently we need to declare the function parameters as pointer types similar to the function swap(), which exchanges the values of the two integer variables pointed to by its arguments.


void swap(int *p, int *q)

 {

    int t;

    t = *p;

    *p = *q;

    *q = t;

    return;
}

Let's call the function swap() by passing values by reference as in the below example:

  #include <stdio.h>

  void swap(int *p, int *q);

  void main ()

 {

    int a = 50;

    int b = 100;

    printf("Before swap-->\n");

    printf("a : %d\n", a );

    printf("b : %d\n", b );

    swap(&a, &b);

    printf("After swap-->\n");

    printf("a : %d\n", a );

    printf("b : %d\n", b );


}

Let's combine the above code in a single C file, compile and execute it, it will produce the following result:
Before swap-->
Value of a :50
Value of b :100
After swap-->
Value of a :100
Value of b :50

<< Back Next >>