passing an argument by reference

M

Michael G

I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.

The values the i need to pass to this function are pointer to shorts.

short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.

Thanks, Mike
 
J

Jack Klein

I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.

The values the i need to pass to this function are pointer to shorts.

short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.

Thanks, Mike

Assuming that x1 and y1 are properly initialized and actually point to
short ints, this is just fine.

What are passed are references to the shorts that the pointers point
to.
 
T

Thomas Wintschel

Michael G said:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.

The values the i need to pass to this function are pointer to shorts.

short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.

Thanks, Mike

The function call is correct provided x1 and y1 actually point to
shorts, i.e.

short a;
short b;

short * x1 = &a;
short * x2 = &b;
 
E

E. Robert Tisdale

Michael said:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y);

This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.

*Real* C++ programmers do it like this:

cat main.cc
#include <iostream>

std::pair<short, short> f(double z);

int main(int argc, char* argv[]) {
std::pair<short, short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}
 
P

Peter Koch Larsen

E. Robert Tisdale said:
Michael said:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y);

This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.

*Real* C++ programmers do it like this:

cat main.cc
#include <iostream>

std::pair<short, short> f(double z);

int main(int argc, char* argv[]) {
std::pair<short, short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}
Well - those two functions are different. Your function has no access
to the initial values of x and y.

/Peter
 
J

jeffc

Michael G said:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y){}
some calculations are done on z and then the results are being passed back
out in x and y.

The values the i need to pass to this function are pointer to shorts.

Well, not exactly. You need to *get* the values from pointers to shorts.
short * x1;
short * y1;

If I call the function like

function(z1, *x1, *y1);

The compiler doesn't complain and I get correct results with the small
number of tests that I have performed on it. But I am not sure that this is
correct.

It is. It might help to look at it like this.

short x2;
short y2;
x2 = *x1;
y2 = *y1;
function(z1, x2, y2);
Make more sense now?
 
E

E. Robert Tisdale

Peter said:
E. Robert Tisdale said:
Michael said:
I have an api that uses reference as arguments.

void function(double z, short &x, short &y);

This is a very bad idea.
some calculations are done on z
and then the results are being passed back out in x and y.

*Real* C++ programmers do it like this:

cat main.cc
#include <iostream>

std::pair<short, short> f(double z);

int main(int argc, char* argv[]) {
std::pair<short, short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}

Well - those two functions are different.
Your function has no access to the initial values of x and y.

Mike's original description of the API
describes x and y as outputs -- not inputs.
But here is a version which passes them as inputs:
cat main.cc
#include <iostream>

std::pair<short, short> f(short x, short y, double z);

int main(int argc, char* argv[]) {
short x = 7, y = 13;
std::pair<short, short> p = f(x, y, 33.0);
x = p.first;
y = p.second;
std::cout << x << ", " << y << std::endl;
return 0;
}
 
I

Ioannis Vranos

E. Robert Tisdale said:
Mike's original description of the API
describes x and y as outputs -- not inputs.
But here is a version which passes them as inputs:
cat main.cc
#include <iostream>

std::pair<short, short> f(short x, short y, double z);

int main(int argc, char* argv[]) {


You don't need argc and argv here.


short x = 7, y = 13;
std::pair<short, short> p = f(x, y, 33.0);
x = p.first;
y = p.second;
std::cout << x << ", " << y << std::endl;
return 0;
}


Mine is better.


#include <iostream>

template<class A, class B, class C>
std::pair<A, B> f(A x, B y, C z);

int main()
{
short x = 7, y = 13;
std::pair<short, short> p = f(x, y, 33.0);

x = p.first;
y = p.second;

std::cout << x << ", " << y << std::endl;
}



And happiness never ends.






Ioannis Vranos
 
H

Howard

E. Robert Tisdale said:
This is a very bad idea.

I'm guessing from the original post, that this is not something he gets to
choose. He says "I have an api that..", from which I gather that he's using
an existing api, not writing one.

Besides, his question is not about the design of the call, but how to use it
properly.
some calculations are done on z
and then the results are being passed back out in x and y.

*Real* C++ programmers do it like this:

cat main.cc
#include <iostream>

std::pair<short, short> f(double z);

int main(int argc, char* argv[]) {
std::pair<short, short> p = f(33.0);
std::cout << p.first << ", " << p.second << std::endl;
return 0;
}

Wow. And here I thought I *was* a *real* C++ programmer. I guess I'm
imaginary. (So please feel free to ignore me. :)) I doubt that I'd
create a whole new type simply for the purpose of getting a pair of values
out of a function. If my program was using such a type already, sure...that
would work great. Otherwise, it's kind of overkill.

But, what I might question is whether the OP really needs pointers to shorts
in the first place. If he's doing that because of the signature of this
function, that's not what's needed. Simply passing the variables themselves
is appropriate. But if he has pointers to shorts anyway, and needs to pass
their values to this function, then he's doing it correctly.

-Howard
 
E

E. Robert Tisdale

Howard said:
Wow. And here I thought I *was* a *real* C++ programmer.
I guess I'm imaginary. (So please feel free to ignore me. :))
I doubt that I'd create a whole new type
simply for the purpose of getting a pair of values out of a function.

No. But I would ask myself,
"Why would I get two unrelated values back from a function call?"
"Aren't they really related parts of a single object?"
And then I would define a class to represent that object.
If my program was using such a type already, sure...that
would work great. Otherwise, it's kind of overkill.

But, what I might question is
whether the OP really needs pointers to shorts in the first place.

He does not.
If he's doing that because of the signature of this function,
that's not what's needed.
Simply passing the variables themselves is appropriate.
But if he has pointers to shorts anyway,
and needs to pass their values to this function,
then he's doing it correctly.

I'm sure that everyone agrees.

But the API is the root of the problem.
We can't tell from the signature alone:

void f(double z, short& x, short& y);

whether x and y are inputs that need to be initialized
before calling this function or whether they are simply outputs
which can be passed to f(z, x, y) uninitialized.
A *real* C++ programmer would avoid this ambiguity by

std::pair<short, short> f(short x, short y, double z);

returning the pair by value instead.
 
M

Michael G

Howard said:
But, what I might question is whether the OP really needs pointers to shorts
in the first place. If he's doing that because of the signature of this
function, that's not what's needed. Simply passing the variables themselves
is appropriate. But if he has pointers to shorts anyway, and needs to pass
their values to this function, then he's doing it correctly.

Yeah. I was given an api with references as parameters and I was also
suppose to place this new api function into some existing code. My superior
frowns upon additional declaration, etc. so I was just trying to make due
with what I was handed. And of course I want it to be correct.

Thanks to everyone for the disscussion.
Mike
 
M

Michael G

jeffc said:
Well, not exactly. You need to *get* the values from pointers to shorts.


It is. It might help to look at it like this.

short x2;
short y2;
x2 = *x1;
y2 = *y1;
function(z1, x2, y2);
Make more sense now?

Yes it does.
Thanks.
Mike
 
O

Old Wolf

Ioannis Vranos said:
#include <iostream>

template<class A, class B, class C>
std::pair<A, B> f(A x, B y, C z);

int main()
{
short x = 7, y = 13;
std::pair<short, short> p = f(x, y, 33.0);

It would be nice if the language didn't require you to repeat
the type of p. It is obviously deducible from f, something like:
_Let p = f(x, y, 33.0);
Has this ever been proposed?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top