Sign in to follow this  
Genesiiis

Function paramaters

Recommended Posts

Hi there. Below is a small bit of code to help be ask my question. --------------------------------------------- # include <iostream> using namespace std; int change_number(int num_1_p, int num_2_p); int main() { int num_1 = 20; int num_2 = 40; cout << num_1 << "\n"; cout << num_2 << "\n\n"; change_number(num_1, num_2); cout << num_1 << "\n"; cout << num_2 << "\n"; for(;;); return 0; } int change_number(int num_1_p, int num_2_p) { cout << "--------------------------\n\n"; for(int i=0; i<3; i++) { cout << num_1_p << "\n"; cout << num_2_p << "\n\n"; num_1_p--; num_2_p--; } cout << "--------------------------\n\n"; cout << num_1_p << "\n"; cout << num_2_p << "\n\n"; cout << "--------------------------\n\n"; return 0; } --------------------------------------------- Usually when in a function I would assign the argumaents that were pased to it to a local variable within the function, for example if I one argument was 10 and the paramater of the function that the argument was for was num then I would assign num to a local variable within the fuction then modify the variable that was assigned num's value like below. --------------------------------------------- fun(10); int fun(int num) { i = num; --------------------------------------------- However in the origional code I included, in the function change_number I didnt assign the values of the paramaters to any local variable withing the function but I can modify the paramaters still, and can modify them as if I had assigned their value to a local variable. When modifying the paramaters aswell, they do not modify the actual arguments of the function. As shown in the origional code I cout the variables num_1 and 2, then entered the function, and I used as arguments those variables, but the function did not actually affect the variables as shown when their values were cout again at the end they were the same as the begining. Is this just a feature that C++ has and are there limitations to its use, for instance will it not work with doubles or something like that. Thanks.

Share this post


Link to post
Share on other sites
Whenever you pass parameters to a function a copy of this variable is created
on the stack frame of the function, In other words function parameters are like regular local variables. So changing them won't affect the original variable.

If you want to change the original passed variable you should either use pointers
or references.
Example with references :


# include <iostream>
using namespace std;

int change_number(int &num_1_p, int &num_2_p);

int main()
{
int num_1 = 20;
int num_2 = 40;

cout << num_1 << "\n";
cout << num_2 << "\n\n";

change_number(num_1, num_2);

cout << num_1 << "\n";
cout << num_2 << "\n";

for(;;);

return 0;
}

int change_number(int &num_1_p, int &num_2_p)
{
cout << "--------------------------\n\n";

for(int i=0; i<3; i++)
{
cout << num_1_p << "\n";
cout << num_2_p << "\n\n";

num_1_p--;
num_2_p--;
}

cout << "--------------------------\n\n";

cout << num_1_p << "\n";
cout << num_2_p << "\n\n";

cout << "--------------------------\n\n";

return 0;
}



Note the & sign in the change_number function declaration.

Cheers.

Share this post


Link to post
Share on other sites
There are three ways that arguments can get passed in to a function in C++: by pointer, by reference, and copy. The first two, you don't need to worry about yet, suffice it to say that you aren't doing that yet. What your functions are doing isn't actually passing the variable (that would be by reference), it's passing a copy of the variable. If you pass foo into some function, the function never has control of foo, it just gets another variable that has the same value of foo. However, as you have noticed, changing the value of this new variable doesn't affect foo. That's because they aren't the same variable, they are simply copies of eachother. This copying only happens once; when the function is called. Beyond that, they are two separate variables.

It's similar to this:

int foo = 8;
int bar = foo;
bar = 16;
//foo is still 8 here


And FYI, you can wrap your code inside of [[u][/u]code] and [[u][/u]/code] tags for smaller bits of code (eg: your second bit of code), or [[u][/u]source] and [[u][/u]/source] tags for larger bits of code (your first bit).

Hope that helps.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this