OutParametres

Hello everyone I am so sorry to engage you with my absurt questions.But I have a question mark about outparametres.Up to me,we use outparametres for giving more than one value to our variebles.But when we do that, will the origin of our varibles change?For ex,in this example will the value of our integre change?


And the values whic has & in front of them are outparametres?for ex (const float& Damage).

You don’t give more than one value to your variables. The variable, in your example above, can only hold 1 value. I think you mean if you want to return more than one value from a function.

For example, takes a single value and returns it

int square(const int value) 
{
  return value*value;
}

In this case, because you return a single value, you don’t need to use a reference, you can just return it. This leaves your input intact so

int value = 3;
int result = square(value);

result will be 9 and value will remain as 3.

But lets say you want to work out the circumference and area of a circle but only using a single call…

void circle_details(const float radius, float& area, float& circumference)
{
  area=3.14*radius*radius;
  circumference = 2*3.14*radius;
}

float radius = 3;
float area = 5;
float circumference = 10;
circle_details(radius,area,circumference);

Radius will be untouched - it is a const and cannot be modified anyway. Area would become 28.26 and circumference would become 18.84, replacing their initial assigned values.

So, to be clear, you are not giving more than one value to your variable, it enables you to return more than one result from a function. A variable can only have a single value, at least a simple type such as a float or an int.

I’ve set up this link for you to try online via godbolt.org - experiment with the input values and you can see how they are affected: Compiler Explorer
I hope this clears things up for you

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms