C Function that Implements Call-By-Reference

The main() function in a C program calls other functions. Some functions need arguments and some do not need any arguments. The efficiency of the program sometimes depends on what and how the arguments passed into the function.

Advertisements

This program is written in Dev-C++ version 4 installed on a Windows 7 64-bit system, but you can use any other standard C compiler to compile and run this program.

The program “C function that implements call-by-reference” is intended for beginner and intermediate level learner of C programming. You may need prior knowledge of pointers to understand this program.

Problem Definition

There are two methods of calling a function in the main() function of a C program.

  1. Call-by-Value
  2. Call-by-Reference

In this post, we will only discuss the call-by-reference method. To learn about call-by-value method, read the article below.

C Function that Implements Call-By-Value Method

In call-by-reference method, when the main() function calls another function the actual arguments are not sent, but a reference to the original values of the variable is sent to the function.

The function will use the memory address (the reference) of the original value of the variable and perform operations on it. You must carefully use this method because the original value can be changed during the execution of the program.

For example, we will declare two variable a and b

Advertisements

int a = 3;                          /* Original Values */
int b = 4;                          /* Original Values */

int *p, *q;                        /* Declare two pointers */

p = &a;                             /* Pointer p refers to the memory address of the variable a */
q = &b;                             /* Pointer q refers to the memory address of the variable b */

swap ( p, q );                    /* Pass the memory address of the original value in the function */

Now, in the function swap (), we use the reference to the original value and do the necessary operations.

void swap ( int &a, int &b )
{

         ……………………………..
        ……………………………..
        ……………………………..
}

Flowchart

Flowchart: C Function that Implements Call-By-Reference Method
Flowchart: C Function that Implements Call-By-Reference Method

Program Code

/*PROGRAM TO DEMO CALL-BY-REFERENCE */ 

#include <stdio.h> 
#include <conio.h> 
main() { 
int a,b,i; 
void swap(int *p,int *q); 
int *p, *q; p = &a; q = &b; 

/* Read two Integers */ 

printf("Enter two number A and B:"); 
scanf("%d%d",&a,&b); 

for(i=0;i<35;i++) 
printf("_");printf("\n\n"); 

printf("Before Swap : %d\tB = %d\n",a,b); 

for(i=0;i<35;i++) printf("_");
printf("\n\n"); 

/* Swap the number using function swap() that uses original values of variable A and B */ 

for(i=0;i<35;i++) 
printf("_");printf("\n\n"); 

swap(p,q); for(i=0;i<35;i++) 

printf("_");printf("\n\n"); 

getch(); 
return 0; } 

/* Swap Operations */ 


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

int temp = 0; temp = *p; 
*p = *q; 
*q = temp; 

printf("After Swap : A = %dtB = %dn",*p,*q); 

}

 

Output

The output of the program is given below.

Output : C Function that Implements Call-By-Reference Method
Output : C Function that Implements Call-By-Reference Method

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.