0

How can you print a variable before you add a value to it?

This is the code that works #include <iostream> using namespace std; void printSomething(int x) { cout << x; } int main() { printSomething(42); } But shouldn't the code be #include <iostream> using namespace std; void printSomething(int x) { } int main() { printSomething(42); cout << x; }

2nd Sep 2016, 4:57 PM
Jasmine
Jasmine - avatar
2 Answers
0
in the second code printSomthing(int x){ } does litterally nothing .. it has no implementation so it does nothing .. you may try this code #include <iostream> using namespace std; void PrintSomething(int newvalue, int &x) { x = newvalue; } void main() { int x; PrintSomething(42,x); cout << x; }
4th Sep 2016, 9:17 PM
Eslam Radi
Eslam Radi - avatar
0
in this case I recommend the function call should be ChangeValue instead of PrintSomething .. unless you stick with your first code such that PrintSomething actually does cout something. I hope that helped.
4th Sep 2016, 9:20 PM
Eslam Radi
Eslam Radi - avatar