Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // More tutorials at BrandonHok.com
- #include <iostream>
- // C++ Crackdown - 3.4 Passing Arguments
- // As you have noticed that we have been passing arguments to our functions?
- // Yep, you've learned a lot already! We were passing by values by using int in our functions.
- // This time we will pass by references and see the differences between the two!
- // Do you know what are these statements below called?
- // Section 1
- // Type the code below until the next section and answer the comments on a separate paper or within your compiler.
- void referenceMan(int &);
- int valueWoman(int power);
- int main()
- {
- int reference = 1337;
- int value = 555;
- std::cout << "This is the original reference: " << reference << std::endl;
- referenceMan(reference);
- std::cout << "This is the passed reference: " << reference << std::endl;
- // Section 2
- // Run everything in section 1 before running these lines and watch what happens
- //std::cout << "This is the original value: " << value << std::endl;
- //std::cout << "This is the passed value: " << valueWoman(value) << std::endl;
- //std::cout << "This is the value variable after passing: " << value << std::endl;
- system("pause");
- }
- void referenceMan(int &reference)
- {
- reference = reference * 10;
- }
- int valueWoman(int power)
- {
- int powerup = power * 10;
- return powerup;
- }
- // Review Questions
- // Can you explain to my mom how the entire code above worked? KEEP IT SIMPLE. MOM NEEDS TO KNOW.
- // What are the main differences between passing by reference and by value? Did you see the difference between referenceMan and valueWoman?
- // 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