Pages: [1]   Go Down
Author Topic: Pass a value to a function from a function  (Read 615 times)
0 Members and 1 Guest are viewing this topic.
Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As the title says, is it possible?

I have this line of code,

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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3420
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Check out my experiments http://blog.blinkenlight.net

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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,

Code:
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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!
« Last Edit: May 02, 2012, 05:12:52 pm by wildbill » Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Scunthorpe, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 129
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

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.
« Last Edit: May 04, 2012, 04:17:44 am by Pavilion1984 » Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 51
Posts: 3438
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: