Pass a value to a function from a function

As the title says, is it possible?

I have this line of code,

if (ELM_Read() == 1)                           //Calls the ELM_Read block.

so am returning a value from ELM_Read but i all so want to pass a vaule of 2 to the ELM_Read function todo some checks.

Each block that calls the ELM_Read function will be a little different so the value 2 may change to 1.

Thanks.

Yes, of course it is possible. You just have to declare ELM_Read() accordingly, e.g. like this:

int EL_Read(int value) {
    return value;
}

This function would just return the value you pass to it. But I think it should give you the general idea.

I might not have been clear enough lol. I have this block of code,

int getERPM()                                    //Engine RPM.
{
  int result;                                    //Variable to store calculation result.
  Serial.print(ENGINE_RPM);
  if (ELM_Read() == 1)                           //Calls the ELM_Read block.
  {
    result = ((Byte_AH * 256) + Byte_BH) / 4;    //Function calculation.
    return result;
  }
  return 0;
}

that is being called from my main loop. So inside this block i have,

if (ELM_Read() == 1)                          //Calls the ELM_Read block..

I want to pass the value of 2 to the ELM_Read() function. Is that possible?

Many thanks.

I want to pass the value of 2 to the ELM_Read() function. Is that possible?

Yes. Declare it as Udo Kline suggested (correct the EL/ELM typo)
Call it like this:

if (ELM_Read(2) == 1)                           //Calls the ELM_Read block.

Use the two for whatever you need in ELM_Read and return whatever value you need.

Edit: typo of my own!

Many thanks for your replys.

Ok i see now but what if i want to replace the number 2 with a name like "rlengh"?

Thanks.

You can pass whatever parameter you like to a function - you just need to ensure that the function declaration has parameters of an apropriate type. It might be easier to help if you can post your complete sketch and give a bit more detail of what you're trying to do.


I want to use the value i pass to ELM_Read() i.e 2 that means i should have received 2 bytes of data back and 1 for 1 byte back as a way of checking and sorting the bytes of data into the correct places.

Thanks.

Looks like you've cracked it, although I don't see anywhere there where you're passing anything other than 2 yet. If you want to give the parameter you pass a name, use #define or const byte.