Advertisement
bhok

3.4 Functions References and Values

Jul 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. // More tutorials at BrandonHok.com
  2.  
  3. #include <iostream>
  4.  
  5. // C++ Crackdown - 3.4 Passing Arguments
  6.  
  7. // As you have noticed that we have been passing arguments to our functions?
  8. // Yep, you've learned a lot already! We were passing by values by using int in our functions.
  9. // This time we will pass by references and see the differences between the two!
  10.  
  11. // Do you know what are these statements below called?
  12.  
  13. // Section 1
  14. // Type the code below until the next section and answer the comments on a separate paper or within your compiler.
  15.  
  16. void referenceMan(int &);
  17. int valueWoman(int power);
  18.  
  19.  
  20. int main()
  21. {
  22.     int reference = 1337;
  23.     int value = 555;
  24.  
  25.     std::cout << "This is the original reference: " << reference << std::endl;
  26.     referenceMan(reference);
  27.     std::cout << "This is the passed reference: " << reference << std::endl;
  28.    
  29.     // Section 2
  30.     // Run everything in section 1 before running these lines and watch what happens
  31.     //std::cout << "This is the original value: " << value << std::endl;
  32.     //std::cout << "This is the passed value: " << valueWoman(value) << std::endl;
  33.     //std::cout << "This is the value variable after passing: " << value << std::endl;
  34.     system("pause");
  35. }
  36.  
  37. void referenceMan(int &reference)
  38. {
  39.     reference = reference * 10;
  40. }
  41.  
  42. int valueWoman(int power)
  43. {
  44.     int powerup = power * 10;
  45.     return powerup;
  46. }
  47.  
  48. // Review Questions
  49.  
  50. // Can you explain to my mom how the entire code above worked? KEEP IT SIMPLE. MOM NEEDS TO KNOW.
  51. // What are the main differences between passing by reference and by value? Did you see the difference between referenceMan and valueWoman?
  52.  
  53. // Can you create a function that passes by reference? How about a pass by value function? Try it once and try it without looking.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement