pass function paramaters by reference

Hello, I created the following code to pass the the variable 'inputVoltage' by reference to the function 'input'. It certainly seems to work when I run the program, but have I actually done it correctly ?

Thanks for any confirmation.

int main ( {
double inputVoltage;
input(&inputVoltage);
return 0;
}

int input(double* inputVoltage)
{
cin >> *inputVoltage;
etc.
etc.
}
I hope your original code is valid. As for the function if you are going to pass arguments by reference you should declare function as

int input(double &inputVoltage);

and call it as

input( inputVoltage );

Last edited on
yes, I did declare the function like this: 'int input(double *inputVoltage);'
My version worked. i understand it is not conventional, so how come it still worked ?
What was I actually doing with my implementation ?

Thanks for any explanation.
Last edited on
You are using a pointer to a variable. So you are changing the original object because you are passing a pointer to it.
thanks, so just to confirm, is it better to 'pass an argument as reference'
'rather than 'passing pointers as reference' ?

Thanks.
Last edited on
They both work. Except it is very easy to confuse the * 's and & 's when dealing with pointers.

It is much easier to just call input( inputVoltage ); in the main function and only have to remember to put one & in the function definition
Topic archived. No new replies allowed.