Call by Reference in C

In Call by Reference, the address of the actual parameter is passed to the function. This means changes made to the formal parameter inside the function do affect the actual parameter.  It’s used when you want the function to modify the original data. To pass the address, use the address-of operator &.

Actual parameters are the values or variables passed to the function when it is called. Formal parameters are the variables defined in the function declaration and definition that receive the values of the actual parameters.

Call by Reference is a method of passing arguments to a function in which the actual memory address of the argument is passed, rather than a copy of the argument.

Let us see an example of implementing Call by Reference in C programming.

In the function changeValue(), the parameter int *x is a pointer to an integer. The * operator was also used to declare a pointer variable.

The address of is passed to the function using the address-of operator &. Inside the function, the * operator is used to dereference the pointer x, allowing direct access and modification of the value a:

Output

The above example shows how the custom function  changeValue()  directly modifies the value of a by using the memory address, effectively achieving call by reference.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Call by Value in C
Data Types in C
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading