0

I have a function SetTFgr(float T, int FGr) to set the time and filtergrade of my LED enlarger.

It works without a problem and changes the values of T or Fgr according to the buttons I press. Both T and FGr are global variables.

Now I want to use the same funtion with different variables. When I call the function with SetTFgr(TExp[i], FGrExp[i]) I would expect the function to work with the values of TExp[i] or FGrExp[i] and increase/decrease these values. It does not, it appears to use the value of T or Fgr instead.

How can I get what I am after?

2
  • 3
    Please show a full code example with your problem
    – chrisl
    Commented Apr 25, 2020 at 8:38
  • So to be clear, you want to read the values of buttons (meaning unspecified) and use those values to set a float and an int variable. Further, you want to write a function that takes a couple of parameters that specify where you want to store those button values?
    – Duncan C
    Commented Apr 25, 2020 at 13:04

2 Answers 2

1

To be clear, you want to read the values of buttons (meaning unspecified) and use those values to set a float and an int variable. Further, you want to write a function that takes a couple of parameters that specify where you want to store those button values?

That is an unusual way to use function parameters. Usually function parameters specify an input value, and then you use that value to do something. Instead, you want to have the inputs be globally available readings (button values) and have the function parameters specify where to store those values.

C and C++ function parameters are usually passed "by value". That means that what gets sent to the function is a value, not a pointer to the variable that contains that variable. Thus the called function can't change the original variable.

If you want to pass a pointer to the variable, you have to use the & "address of" operator to pass the variable "by reference".

Your function might look like this using C++ pass by reference syntax:

void SetTFgr(float &T, int &FGr) {
    float input_T = //figure out button value for T
    T = input_T;
    int input_FGr = //figure out button value for Far
    FGr = input_FGr;
}

Then you'd invoke it like this:

SeTFgr(T, FGr);

You could also accomplish the same thing using C pointer syntax.

0

Parameters of functions in C++ are by value. So T in your function is not the variable you use to call the function. It only has the same value. Use float& T to pass the reference to the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.