C programming function arguments

C programming function arguments also known as parameters are the variables that will receive the data sent by the calling program. These arguments serve as input data to the function to carry out the specified task.

c programming function arguments


Description of C programming function arguments

function arguments

Here, as shown in the figure above arguments_value is used to send values to the called program.

Learn about user defined function in C programming.

Function arguments in c programming

Basically, there are two types of arguments:

  • Actual arguments
  • Formal arguments

The variables declared in the function prototype or definition are known as Formal arguments and the values that are passed to the called function from the main function are known as Actual arguments.

The actual arguments and formal arguments must match in number, type, and order.

Following are the two ways to pass arguments to the function:

  • Pass by value
  • Pass by reference

Pass by Value

Pass by value is a method in which a copy of the value of the variables is passed to the function for the specific operation.

In this method, the arguments in the function call are not modified by the change in parameters of the called function. So the original variables remain unchanged.

Example of passing arguments by value to function in C

// arguments pass by value 
# include <stdio.h>
int add (int a, int b)
    return( a + b ); 
int main()
   int x, y, z;
   x = 5;
   y = 5;
   z = add(x,y); // call by value
return 0;
//end of program

In this program, function add() is called by passing the arguments x and y.

The copy of the values of x and y are passed to a and b respectively and then are used in the function.

So by changing the values of a and b, there will be no change in the actual arguments x and y in the function call.

Pass by reference

Pass by reference is a method in which rather than passing direct value the address of the variable is passed as an argument to the called function.

When we pass arguments by reference, the formal arguments in the called function becomes the assumed name or aliases of the actual arguments in the calling function. So the function works on the actual data.

Example of passing arguments by reference to function in C

// arguments pass by reference
 #include <stdio.h>
 void swap (int *a, int *b) // a and b are reference variables
    int temp;
    temp = *a; 
    *a = *b;
    *b = temp;
 int main()
    int x = 2, y = 4;
    printf("before swapping x = %d and y = %d\n", x, y);
    swap(&x, &y);     // call by reference
    return 0;
 } //end of program

In the above program, the formal arguments a and b becomes the alias of actual arguments x and y when the function was called.

So when the variables a and b are interchanged x and y are also interchanged. So the output becomes like this.


before swapping x = 2 and y = 4
after swapping x = 4 and y = 2

Now, if the function was defined as:

void swap(int a, int b)
    int temp;
    temp = a;
    a = b;
    b = temp;

This is the pass by value method so here even if the values are swapped in the function the actual value won’t interchange and output would become like this:

before swapping x = 2 and y = 4
after swapping x = 4 and y = 2