Passing variables to functions problem?

I've come across pointers and I've moved on to passing pointers to functions. I decided to make my own program related to this so I can understand the concept. Problem is I can't. Here's the code:
____________________________________________________________

#include<iostream>
using namespace std;

int function(int int1)
{
int1=10;
cout<<int1<<endl;
return int1;
}

int main()
{
int int2=5;
function(int2);
cout<<int2;
}
____________________________________________________________

The result is:
____________________________________________________________
10
5
___________________________________________________________

What I believe the program is supposed to do is (when compiled):

1.Identify the function
2.Identify that int2's value is 5
3.Then, once it's called, change the value of int1 to 10,(in the function)
4.And then,(heres where I think Im wrong) take the value from the function (which is 10) and cout 10 (in int main()) , because the value of int2 now holds 10.

If this isn't the case, what's the point of passing values?

Can someone explain how this is really supposed to work?
Last edited on
When you pass a variable by value to a function, it makes a copy of that value - it doesn't send the variable itself.

So, for example:
1
2
3
4
a = 5;
cout << a;
someFn(a); 
cout << a;


would make a = 5, display the value of 'a' (which is 5), send a copy of 'a' (which is still 5) to someFn(), and then display 'a' again -- which would still be 5, because someFn() didn't do anything to a itself. It only did something to a copy of 'a'.

------------

In your program, a copy of int2 is sent to function(), so function receives the value 5. Because it is written as function(int int1), the value received is given the name int1. Then you set int1 to 10, and then display that value. Then you return int1 -- but that doesn't do anything because nothing will contain the value returned by function(). ( If you wanted int2 to hold the value returned by int1, you would need to write int2 = function(int2); )

Now, remember that function() received a copy of int2, not the variable itself. So int2 is still unchanged by being passed by value. Thus, displaying the value of int2 displays a 5.

------------

When you pass a pointer to a function, you are giving the function a copy of the location of the variable, so that it can do something to the value at that location. This is a way to change the variable from within the function.
As I understand it, you want to test passing a pointer to a function? However, you don't have any pointers defined? You might want to define the variable you pass to the function and the parameter defined in the function as pointers?
Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;

//reference
int function(int& int1)
{
int1=10;
cout<<int1<<endl;
return int1;
}

//or

//pointer
int* function(int* int1)
{
*int1=10;
cout<<*int1<<endl;
return int1;
}

int main()
{
int int2=5;
function(int2);//reference
function(&int2);//pointer
cout<<int2;
}

Use either references (&), or pointers.
Last edited on
Topic archived. No new replies allowed.