You don't need to specify the type on an actual parameter only on the formal parameter:
Code:
#include <iostream>
using namespace std;
void function(int x);
void function(int x)
{
x = 5;
cout<<x;
cin.get();
}
int main()
{
int x;
function(x);
}
This passes a copy of x to function(). Since its a copy any changes made to x inside of function() do not affect the x inside of main(). This is known as passing by value.
To have function() affect a change upon the variable x in main() you need to pass a reference. To do that you have to change function() to accept a reference like so:
Code:
void function(int &x)
Now any changes made to x inside of function() affect the variable that was passed into it. Its important to note that the names do not matter. To see this try running the following:
Code:
#include <iostream>
using namespace std;
void function(int x);
void function(int x)
{
x = 5;
cout<<x<<endl;
}
int main()
{
int z;
function(z);
cout<<z<<endl;
cin.get();
}