Use local variable in another function

Hi there, I was confused on how to use local variable of a function in the other function.. I am involving related parts below:

char radiopacket[25];

void send()
{
  rf95.send(radiopacket, strlen(radiopacket)+1);
  

  rf95.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);
}

void receive
{
 if (rf95.recv(buf, &len))
   {
      Serial.print("Got reply: ");
      Serial.println((char*)buf)

}
void loop
{
send();
receive();

}

It is showing error of "len was not declared in this scope " for function "receive" there..
Pls help me out, thanks!Preformatted text

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

—-

You can’t use a local variable in another function. Make it global or return it or pass that as a parameter

Do you mind to show how to return or pass that as parameter?
I get confused on tat..
I am sorry that I am still not familiar with the code tags n those..

I might When you have fixed your first post

1 Like

That's why @J-M-L provided you with a very handy link for you to learn them.

1 Like

I'd say for you, the best choice would be to make 'buf' and 'len' global variables.

I did try to put as global variables in void loop, but same error coming out

Put it where you have 'radiopacket' defined.

It is now showing conflicting declaration even though it only appears once there

Done all I can given the limited information you've supplied. Might be able to help more if you posted the complete code and GitHub links to any libraries you're using. Be good if you identified which Arduino board also.

1 Like

Here is an example. But you should read a programming tutorial to get a fine understanding of functions, parameters, types etc

int getReturnValue() {
  return 42;
}

void getReferenceValue(int& x) {  // pass by reference, notice the &
  x = 42;
}

void setup() {
  int v1=0, v2=0;
  v1 = getReturnValue();  // now v1 is 42
  getReferenceValue(v2);  // now v2 is 42
}

void loop() {}

Could you further explain what is the use of & ?
I am thinking if I can apply in my case too..

Read first about functions

Then

But your code should have the variables where they belong…


char radiopacket[25];

void send() {
  rf95.send(radiopacket, strlen(radiopacket)+1);
  rf95.waitPacketSent();
}

void receive() {
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = RH_RF95_MAX_MESSAGE_LEN;
   if (rf95.recv(buf, &len)) {
     Serial.print("Got reply: ");
     Serial.println((char*)buf); // are you sure a trailing null was sent ??
  }
}

void setup() {}

void loop() {
  send();
  receive();
}

1 Like

Alright, but what if I need that variable in another function too? for example, I want to return more than one value from void send into void loop first, then I use it in void receive?
I know that the void should be changed to either int/char/others accordingly, am I right?

Make them global is the easiest answer.

You can return multiple values in a struct and/or have multiple parameters. You can also have pointers.

In practice it’s also probably about organizing the code in the right way.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.