Advertisement
avv210

passByValue and passByReference

Feb 26th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. // SortingAlgorithm.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. #include <iostream>
  3.  
  4. void swapByValue(int el1, int el2)
  5. {
  6.     // Swapping algorithm
  7.     int temp = el1;
  8.     el1 = el2;
  9.     el2 = temp;
  10.  
  11.     /*std::cout << "el1: " << el1 << "\n\n";
  12.     std::cout << "el2: " << el2 << "\n\n";*/
  13. }
  14.  
  15. void swapByReference(int& el1, int& el2)
  16. {
  17.     int temp = el1;
  18.     el1 = el2;
  19.     el2 = temp;
  20. }
  21.  
  22. int main()
  23. {
  24.     int num1 = 5;
  25.     int num2 = 6;
  26.    
  27.     // Before swapping
  28.     std::cout << "Before Swap\n";
  29.     std::cout << "num1: " << num1 << "\n";
  30.     std::cout << "num2: " << num2 << "\n\n\n";
  31.  
  32.     // After swapping
  33.     std::cout << "After Swap\n";
  34.     // call function here
  35.     // swapByValue(num1, num2);
  36.     swapByReference(num1, num2);
  37.     std::cout << "num1: " << num1 << "\n";
  38.     std::cout << "num2: " << num2 << "\n";
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement