What is the side effect in the following function?

int ExamPrep (int param1, int& param2)
{
   if (param2 =  param1)
     return param2;
   else if (param2 > param1)
     return param1;
   else
     return param1 * param2;
}

A side effect is defined as the result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated. I don't really understand how that is presented in the above code though....

Hi,
The first if statement is the clue for you. If you look closely at it, you will realize that the expression there actually changes data which persists even after the code returns from the function.

Hi,
The first if statement is the clue for you. If you look closely at it, you will realize that the expression there actually changes data which persists even after the code returns from the function.

Ah ok so the side effect lays within line 3. That makes sense now thank-you!

Yeah the first answerer is correct. The side effect is in line 3 because when you pass the parameters into the method you're passing a reference to param2. This means that anything you do to the reference happens to the original variable which may lie in a separate function entirely. Basically, when you set param2 equal to param1 it sets the original copy of param2 through the reference to the variable.

Sorry if it's off-topic, just felt like explaining why the side effect lies there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.