[go: up one dir, main page]

0% found this document useful (0 votes)
78 views2 pages

8.call by Reference

The document discusses call by reference in C programming. It provides an example program that defines a swap function that takes integer pointers as arguments. The addresses of the actual arguments x and y are passed to the swap function. Any changes made to the formal arguments *a and *b in the function affect the actual arguments x and y in main. The output demonstrates that the values of x and y are swapped both in the function and in main after the function call.

Uploaded by

Karthick M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views2 pages

8.call by Reference

The document discusses call by reference in C programming. It provides an example program that defines a swap function that takes integer pointers as arguments. The addresses of the actual arguments x and y are passed to the swap function. Any changes made to the formal arguments *a and *b in the function affect the actual arguments x and y in main. The output demonstrates that the values of x and y are swapped both in the function and in main after the function call.

Uploaded by

Karthick M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

SNS COLLEGE OF TECHNOLOGY, COIMBATORE –35

(An Autonomous Institution)


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Call by reference
• Instead of passing value, the address of the argument will be passed.
• Any changes to the formal argument will affect the actual argument.
Example

#include <stdio.h>
#include<conio.h>
int main()
{
int x,y;
int swap(int*,int*);
clrscr();
printf("\nEnter value of x:");
scanf("%d",&x);
printf("\nEnter value of y:");
scanf("%d",&y);
swap(&x,&y);
printf("\n\nValues in the Main() : x=%d,y=%d",x,y);
getch();
}
int swap(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
printf("\nValues in the User Defined Function : x=%d,y=%d",*a,*b);
return;
}

Output
Enter value of x:5
Enter value of y:6
Values in the User Defined Function : x=6,y=5
Values in the Main() : x=6,y=5
SNS COLLEGE OF TECHNOLOGY, COIMBATORE –35
(An Autonomous Institution)
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Difference between Call by value and Call by Reference

Call by Value Call by Reference

This is the usual method to call a function in


In this method, the address of the variable is passed as
which only the value of the variable is passed as an
an argument
argument
Any alternation in the value of the argument Any alternation in the value of the argument
passed does not affect the function. passed affect the function.
Memory location occupied by formal and
Memory location occupied by formal and actual
actual arguments is same and there is a
arguments is different saving of memory location
Since a new location is created, this method Since the existing memory location is used
is slow through its address, this method is fast
There is a possibility of wrong data
There is no possibility of wrong data
manipulation since the addresses are used in
manipulation since the arguments are directly used in
an expression. A good skill of programming
an application
is required here

You might also like