Method to modify part of a statement?

Hello,

Thanks for reading. Questions about programming are difficult when you’re not even sure that you’re using the correct terminology but here goes.

I’m curious if there is a method of modifying just a portion of a statement (correct terminology?) with an argument to a function.

For example:

Take a function - readPotentiometers();

Add argument - readPotentiometers(int A);

Inside the function (see comments):

void readPotentiometers(int A)
{
  A = analogRead(potA);
  A = constrain(A, 40, 980);
  // Serial << red << endl;  
  if ((prev(A) + 20) < A || (prev(A) - 20) > A)   //    Here is where I would like to add the argument (A) to the end of "prev" for a result of prevA

  {
    prev(A) = A;  // Here also.
    A = map(A, 40, 980, 0, 255);
    Serial << "New value: " << A << endl;
    Wire.beginTransmission(9); // transmit to device #9
    Wire.write(5);
    Wire.write(A); 
    Wire.endTransmission();
  }

I would like the argument to modify the line:
if ((prev(A) + 20) < A || (prev(A) - 20) > A)

to read this when compiled:
if ((prevA + 20) < A || (prevA - 20) > A)

Is this something that’s possible? I’ve ran into a few cases where this would be a really convenient tool.

No all names ect are gone by run time.

Mark

I think you are a bit confused with your code

void readPotentiometers(int A)
{
  A = analogRead(potA);

This receives an integer (from whatever called the function) and saves it into a variable called A.
The next line promptly replaces A with something obtained from analogRead().

That means that whatever information was passed to the program is lost.

You should save analogRead() to another variable (perhaps potPosition ?)

Then you can create an IF statement that does X or Y depending on the value of A.

...R

There are a few ways to get the same effect.

One way is to combine all the state data that your function needs into a struct that you pass in by reference/pointer. This was the function can access whatever parameters you want specific to each input. This seems the best way to me, and is a good step on the path towards a clean object oriented design. (The next step would probably be to convert the struct into a class, and convert the function into a method of that class.)

Another way is to pass all the values the function wants, with pass-by-reference semantics used to use some of them as in+out parameters where necessary.

a static local variable is remembered over function calls

void readPotentiometers()
{
  static int prevValue = -1;  

  int value = analogRead(potA);
  value = constrain(value, 40, 980);

  if ((prevValue  + 20) < value  || (prevValue - 20) > value)  
  {
    prevValue = value ; 
    value  = map(value , 40, 980, 0, 255);

    Serial << "New value: " << value  << endl;

    Wire.beginTransmission(9); 
    Wire.write(5);
    Wire.write(value ); 
    Wire.endTransmission();
  }

Thanks for the replies.

Robin2:
I think you are a bit confused with your code

void readPotentiometers(int A)

{
  A = analogRead(potA);




This receives an integer (from whatever called the function) and saves it into a variable called A.
The next line promptly replaces A with something obtained from analogRead().

That means that whatever information was passed to the program is lost.

You should save analogRead() to another variable (perhaps potPosition ?)

Then you can create an IF statement that does X or Y depending on the value of A.

...R

Ha. You’re right. I tried to throw together an example of what I was try to accomplish before I headed out this morning. I failed, as this part makes no sense. Rather than create a new variable inside the function, I was looking to insert a previously created variable at this location in the function. I’m thinking this isn’t something that’s possible with this approach though.

robtillaart:
a static local variable is remembered over function calls

void readPotentiometers()

{
  static int prevValue = -1;

int value = analogRead(potA);
  value = constrain(value, 40, 980);

if ((prevValue  + 20) < value  || (prevValue - 20) > value) 
  {
    prevValue = value ;
    value  = map(value , 40, 980, 0, 255);

Serial << "New value: " << value  << endl;

Wire.beginTransmission(9);
    Wire.write(5);
    Wire.write(value );
    Wire.endTransmission();
  }

I didn’t know about “static local variable”. That will be very useful.

PeterH:
There are a few ways to get the same effect.

One way is to combine all the state data that your function needs into a struct that you pass in by reference/pointer. This was the function can access whatever parameters you want specific to each input. This seems the best way to me, and is a good step on the path towards a clean object oriented design. (The next step would probably be to convert the struct into a class, and convert the function into a method of that class.)

Another way is to pass all the values the function wants, with pass-by-reference semantics used to use some of them as in+out parameters where necessary.

I hope that someday I understand everything in your post =) It seems like great advice.

Sorry everyone for the hastily and poorly demonstrated question. Using static local variables as suggested above, I think I’ve found a way to accomplish what I was trying to do.

Thanks for the help.

Snowman815901:
Rather than create a new variable inside the function, I was looking to insert a previously created variable at this location in the function.

Either the solution is very simple (as follows) or I don’t understand what you are trying to say.

If you have two optional calculations - perhaps x = 45 * z and x = 45 * m - then you can send a paramter to a function to tell it which to use - for example

void testFunction(byte choice) {
   if (choice == 1) {
          x = 45 * z;
    }
   if (choice == 2) {
       x =  45 * m;
   }
}

The variables z and m could be global variables if they have relevance in code elsewhere.

There is also nothing to prevent you passing all 3 values as parameters -

void testFunction(byte choice, int m, int z) {

…R