Call by reference in C++ Functions

By: Babbar Ankit Emailed: 1674 times Printed: 2153 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Apart from the method discussed in the first tutorial (highlighing the use of pointer arguments) , C++ provides the \'&\'(referential) operator for calling by reference.

Syntax

function prototype
return-type function-name(data-type & arguement_name);
function definition

return-type function-name(data-type & arguement_name)

{

inside the body , the aguement is to be used as any other variable(not as pointer variable)

}
function call

function_name(arguement name);

//the variable is passed like any other variable of its data type and as an address

Example :: SWAP program revisited

#include<iostream>
using namespace std;

void swap_ref(int &a,int &b);
void swap_val(int a,int b);

 int main()
{
int a=3,b=6;
printf(“\\na=%d  b=%d”,a,b);
swap_val(a,b);

printf(“\\na=%d  b=%d”,a,b);
swap_ref(a,b);
printf(“\\n a=%d  b=%d”,a,b);

return 1;
}

void swap_ref(int &a, int &b)
{
//function acceptsa reference
a=a+b;
//to original parameter variable
b=a-b;
a=a-b;
}
void swap_val(int a, int b)
{
a=a+b;
b=a-b;
a=a-b;
}

OUTPUT:
a=3  b=6
a=3  b=6
a=6  b=3

Authors Url: http://www.botskool.com/programming-tutorials


C++ Home | All C++ Tutorials | Latest C++ Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

but it will be very simple if we use pointer in it.

View Tutorial          By: Hot Water Systems at 2009-06-01 03:51:48
2. View Comment

example is good but in c++ we can't use printf().

View Tutorial          By: vikas at 2009-10-26 20:12:08

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Babbar Ankit
Call by reference in C++ Functions
Default arguments in C++
Function overloading in C++

More Tutorials in C++
Two-Dimensional Array Manipulation in C++
Calculate average using Two-Dimensional Array in C++
Compute the square root of the sum of the squares of an array in C++
Matrix using nested for loops in C++
Sorting an array of Strings in C++
Calculating total based on the given quantity and price in C++
Compiling and Linking Multiple Source Files in C++
Enumerations in C++
Program to add two numbers in C++
Comments in C++
while loop in C++
for loop in C++
Programming errors a compiler will detect in C++
if in C++
Using the Built-in Arithmetic Types in C++

More Latest News
Most Viewed Articles (in C++ )
Using Comments in a C++ Program
Dot (.) vs Arrow (->) to access data members in C++
The indirection operator (*) - dereference operator.
Using peek() and putback() with cin in C++
virtual inheritance example in C++
Function overloading in C++
Checking Prime Number in C++
Compute the square root of the sum of the squares of an array in C++
What Is a Pointer in C++?
C++ Sample Program for Allocating, using, and deleting pointers.
this Pointer in C++
C ++ Reference Parameters Versus Java Reference Parameters
Converting C++ Multiple-Inheritance Hierarchies to Java
Operator Precedence in C++
qsort() sample program in C++
Most Emailed Articles (in C++)
Looping with the keyword goto in C++
Default arguments in C++
Compiling and Linking Multiple Source Files in C++
Difference between Procedural, Structured, and Object-Oriented Programming
Tutorial on Classes and Members in C++
for Loops in C++
Using switch Statements in C++
Operator Precedence in C++
How to handle Exceptions in C++
strcpy() and strncpy() sample program in C++
assert() example program in C++
Using peek() and putback() with cin in C++
Function overloading in C++
File in C++ - Writing text to a file in C++
Pass by Reference vs Pass Value in C++ functions