Advertisement
Felanpro

Pass By Value

Jul 5th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. //We have to make a copy of number because number is can only be in the local scope and myNumber in main scope.
  7. void test(int number) //number = 1 //It's taking the value of myNumber and setting it to the variable number.
  8. {
  9.     cout << number << endl;
  10.     number++;
  11.     cout << number << endl;
  12. }
  13.  
  14. int main()
  15. {
  16.  
  17.     int myNumber = 1;
  18.     test(myNumber); //myNumber = number // number is now a copy of myNumber
  19.     cout << myNumber << endl;
  20.  
  21. }
  22.  
  23. //You're essentially creating a copy for number in the test() scope. Then using the copy in main().
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement