Wednesday, April 3, 2013

C++ example for pass by reference

Pass by reference c++ example





// Program to sort two numbers using call by reference. 
// Smallest number is output first.

#include <iostream>
using namespace std;

// Function prototype for call by reference
void swap(float &x, float &y);

int main()
{
   float a, b;
   
   cout << "Enter 2 numbers: " << endl;
   cin >> a >> b;
   if(a>b) 
     swap(a,b); // This looks just like a call-by-value, but in fact
                // it's a call by reference (because of the "&" in the
                // function prototype

   // Variable a contains value of smallest number
   cout << "Sorted numbers: ";
   cout << a << " " << b << endl;
   return 0;
}

// A function definition for call by reference
// The variables x and y will have their values changed.

void swap(float &x, float &y)
// Swaps x and y data of calling function
{
   float temp;

   temp = x;
   x = y;
   y = temp;
}


OUTPUT:

Enter 2 numbers:
89
56
Sorted numbers: 56 89


Other way of writing the above code 

// Program to sort two numbers using call by reference. 
// Smallest number is output first.

#include <iostream>
using namespace std;

// Function prototype for call by reference using pointers
// see http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/argsC++.html
// for further advice.
void swap(float &x, float &y);
void swap2(float *px, float *py);
void swap2b(float *px, float *py);

int main()
{
   float a, b;
   
   cout << "Enter 2 numbers: " << endl;
   cin >> a >> b;
   if(a>b) 
     swap2(&a,&b); // this call makes explicit that we pass pointers
   //     swap2b(&a,&b); // this call makes explicit that we pass pointers
   //     swap(a,b); // this looks just like a call-by-value.

   // Variable a contains value of smallest number
   cout << "Sorted numbers: ";
   cout << a << " " << b << endl;
   return 0;
}

// A function definition for call by reference
// The variables x and y will have their values changed.

void swap(float &x, float &y)
// Swaps x and y data of calling function
{
   float temp;

   temp = x;
   x = y;
   y = temp;
}

void swap2(float *px, float *py) // Here the arguments are pointers
// Swaps x and y data of calling function
{
   float temp;

   temp = *px;  // to find the value associated with the pointer px, use *px
   *px = *py;
   *py = temp;
}

void swap2b(float *px, float *py)
// Swaps x and y data of calling function
{
   float temp;

   temp = px[0];  // A synonym for *px is px[0]
   px[0] = py[0];
   py[0] = temp;
}

OUTPUT:

Enter 2 numbers:
23
45
Sorted numbers: 23 45




---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

---------------------------------------------------------------------------

Searches related to pass by reference c++ example
c++ parameter reference
• what is the difference between pass-by-value vs. pass-by-reference in c/c++
pass by value and pass by reference c programming
c++ pass by reference vs pointer
c++ pass int by reference
c++ function reference
call by value vs call by reference
call by reference c++
Pointers- Pass by reference and value


IT Certification Category (English)640x480

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


Top Online Courses From ProgrammingKnowledge

Python Course http://bit.ly/2vsuMaS
Java Coursehttp://bit.ly/2GEfQMf
Bash Coursehttp://bit.ly/2DBVF0C
Linux Coursehttp://bit.ly/2IXuil0
C Course http://bit.ly/2GQCiD1
C++ Coursehttp://bit.ly/2V4oEVJ
PHP Coursehttp://bit.ly/2XP71WH
Android Coursehttp://bit.ly/2UHih5H
C# Coursehttp://bit.ly/2Vr7HEl
JavaFx Coursehttp://bit.ly/2XMvZWA
NodeJs Coursehttp://bit.ly/2GPg7gA
Jenkins Course http://bit.ly/2Wd4l4W
Scala Coursehttp://bit.ly/2PysyA4
Bootstrap Coursehttp://bit.ly/2DFQ2yC
MongoDB Coursehttp://bit.ly/2LaCJfP
QT C++ GUI Coursehttp://bit.ly/2vwqHSZ