Go Down

Topic: Pass a value to a function from a function (Read 814 times) previous topic - next topic

Pavilion1984

As the title says, is it possible?

I have this line of code,

Code: [Select]
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.

Udo Klein

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

Code: [Select]

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.
Check out my experiments http://blog.blinkenlight.net

Pavilion1984

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

Code: [Select]
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,

Code: [Select]
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.

wildbill

#3
May 02, 2012, 11:55 pm Last Edit: May 03, 2012, 12:12 am by wildbill Reason: 1
Quote
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:
Code: [Select]
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!

Pavilion1984

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.

wildbill

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.

Pavilion1984

#6
May 03, 2012, 12:20 am Last Edit: May 04, 2012, 11:17 am by Pavilion1984 Reason: 1
Code: [Select]


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.

wildbill

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.

Go Up