Write a C++ program that prompts the user to enter an integer x at the keyboard. Your main function should store the user's integer x, then invoke two non-value returning functions as follows:
printVar(x); // Prints the value of x
changeVar(x); // Squares x (x times x), then adds 5 to x
printVar(x); // Prints the new value of x
The printVar function should pass the parameter by value.
The changeVar function should pass the parameter by reference.
This is the code I wrote. Is it correct?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// function example
#include <iostream>
usingnamespace std;
void printVar(int x)
{
cout << "An integer X is:" << x <<endl;
}
void changeVar(int& x)
{
x = x * x;
x = x + 5 ;
}
I notice that this code is exactly the same as the code someone gave you on another thread to solve this problem.
Just in case you therefore want some additional explanation as to how the parameters of those two functions differ, here's a little bit about it - it's quite important :)
(It's the part called "Arguments passed by value and by reference")