• Advertisement
Sign in to follow this  

passing parameters in C++

This topic is 4343 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i have a tiny problem thats boiling my brain. i have a function that supposed to return 2 values. i think iv declared it right but when i come using it it gives me error that too few params used. the function is : double turnNorth(double dueNorthX, double dueNorthY) { dueNorthX = 0.0; dueNorthY += 0.01; return dueNorthX; return dueNorthY; } this is where i use it: if (distanceOneToFour()<2.0) { Cylin3y = turnNorth(); } any advice? thanx

Share this post


Link to post
Share on other sites
Advertisement
Hey bud,

You can't do that in C++. Program flow breaks out of the function at the first return statement encountered. What you are looking to do here is something like this:

void myfunction( double& dueNorthX, double& dueNorthY )
{
dueNorthX = 0.0;
dueNorthY += 0.01;
}


or


void myfunction( double* dueNorthX, double* dueNorthY )
{
*dueNorthX = 0.0;
*dueNorthY += 0.01;
}


The first uses references and the second uses pointers. If you google for both of those you can find out more information. With your code snippet, the value of dueNorthX will be returned, the two variables passed in get copied for the function and therefore the variables you pass in from outside remain unchanged.

With the first example i provided you are passing the variables by reference which means that you have direct access to the variable on the outside of the function. The second example uses pointers so inside the function you have to dereference the pointers with the start to get at the outside variable.

Another option here is to put both of the parameters in a struct and return that, or pass the struct as a reference.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
You declared your function as taking two double parameters, but aren't providing them in your function call.

You do realize that you are passing the parameters by value, therefore the original parameters won't be modified, right?

Also, why do you have two return statements? Only the first one counts.

As it is written, your function will just ignore the actual values of the parameters, and return zero all the time.

Share this post


Link to post
Share on other sites
You can't return two parameters without using some type of datastructure such as an array, or a struct. You also arn't passing in any parameters when you call the function, which is why you are getting that "too few parameters" errors.

What would be easier in this case is if you passed by reference.


void turnNorth(double &dueNorthX, &double dueNorthY)
{
dueNorthX = 0.0;
dueNorthY += 0.01;
}


if (distanceOneToFour()<2.0)
{
turnNorth(Cylin3y.NorthX, Cylin3y.NorthY);
}

Share this post


Link to post
Share on other sites
Yikes. C++ is an imperative programming language (among others). It executes the statements of your function from the first line to the last line, in that order. When the first "return" is encountered, the function returns to the position right after the function was called. Therefore, the 2nd return is never encountered.

In this case, the simplest way to return these values (coordinates) would be to use an object:

struct Coord
{
double x, y;
};

Coord turnNorth( Coord north )
{
north.x = 0.0;
north.y += 0.01;

return north;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by raz0r
std::pair is also an alternative.


boost::tuple is even better.

Only if he's using boost. =)

Share this post


Link to post
Share on other sites
Quote:
Original post by raz0r
Only if he's using boost. =)


You mean there are people who don't? [wow]

Go easy on me, I'm an old man, my heart isn't what it used to be.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by raz0r
Only if he's using boost. =)


You mean there are people who don't? [wow]

Go easy on me, I'm an old man, my heart isn't what it used to be.

LOL! - Personally, I find boost to be a very good tool, but sadly, not everyone uses it! (It should really be standard, hopefully we’ll see something in 0x) =(

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by raz0r
Only if he's using boost. =)


You mean there are people who don't? [wow]


He could be using tr1::tuple.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
He could be using tr1::tuple.


Which has striking similarities to boost::tuple because that's where the standard proposal came from.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement