Variable swapping using function call by reference – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2685 siteicon   siteicon   siteicon  

Variable swapping using function call by reference

Swapping (Interchange) of two variable values using the call by reference technique of function arguments.

Learning Objectives

  • Interchange of values using call by reference arguments based functions.
  • Change two input variables, which can not be done by returning values from function.

Source Code

TC++ #2685


Source Code

Run Output

The original values are :  a = 10, b = 13

The values after swap () are : a = 13, b = 10

Code Understanding

void swap(int &,int &);
Function prototype with two locations for integer storage passed as reference using addressof (&) operator

int a=10,b=13; cout <<“The original values are : “;cout <<” a = “<<a<<“, b = “<<b<<endl;
Two variables set and displayed the values as set.

swap (a,b);
The function swap is called with actual parameters as a and b where instead of parameter value its locations will be passed to the called function

cout <<“nThe values after swap () are : “; cout <<” a = “<<a<<“, b = “<<b<<endl;
The function called actually interchanges the value and thus interchanged values are printed here.

void swap(int &x,int &y)
This is function definition header with two formal parameters passed as reference and not as value.
int temp ; temp = x ; x = y ; y = temp ;
This is the typical swapping logic as can be understood in details at #2493


  • This type of swapping can not be done easily by using call by value and returning as a typical function can return only one value.

Common Errors

  • The swapping sequence should always be maintained for correct swapping otherwise same value may be accidentally put in both variables.

Suggested Filename(s): cbr-swap.cpp


sunmitra| Created: 16-Jan-2018 | Updated: 16-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues