Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # PASSING ARGUMENT TO A FUNCTION BY REFERENCE IN C

• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 406
Comment on it

Passing argument to a function by reference is also know as call by reference. In this method if we change the parameter given in the function will effect the argument. For passing a value by reference, we use pointers. As we use pointers in call by reference, so arguments address are passed rather than the value.

Below is the example of how to pass argument by reference.

```// function definition for swapping two  integer type values a and b
void swap(int *a, int *b)
{
int t;
t = *a;                                    // save the value at address a into t variable
*a = *b;                                    // we put value of b into a
*b = t;                                     // put t into b
return;
}
Now, we call the function swap() by passing original values .
#include <stdio.h>
void swap(int *a, int *b);
int main () {
int x = 5;   //    local variable definition
int y = 6;
printf("Before swap, value of x : %d\n", x );
printf("Before swap, value of y : %d\n", y );
//  Now we are calling a function to swap two values
swap(x, y);
printf("After swap value of x : %d\n", x );
printf("After swap value of y : %d\n", y );
return 0;
}
```
```**OUTPUT**
Before swap value of x :5
Before swap value of y :6
After swap value of x :6
After swap value of y :5
```

This shows that if we change parameter which are given inside a function will effect the argument

## 0 Comment(s)

OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...