function returning an ip address

I am trying to make a function getIP which when called returns an IP address

caller:

IPAddress anIP = getIP();

callee

IPAddress getP(){
IPAddress ip (1,2,3,4);
return ip;
}

What am I doing wrong?

Thanks for your help!

What am I doing wrong?

Not posting a complete program

I can make no sense of the snippets that you posted

What's the error?

How is IPAddress defined?

I now realize the importance of posting the problematic code by itself! My appologies

Here is the code … and it works perfectly well!

#define sp Serial.print
#define spln Serial.println

#include <SPI.h>
#include <Ethernet.h>

void setup() {
  Serial.begin(38400);
  IPAddress myIP=getIP();
  sp("returned ip is ");spln(myIP);
}

void loop(){;}


IPAddress getIP(){
  byte byteArray[4]={0,1,2,3};
  IPAddress ip (byteArray[0], byteArray[1], byteArray[2], byteArray[3]);
  sp("ip in callee is "); spln(ip);
  return ip;
}

guy_c:
I now realize the importance of posting the problematic code by itself! My appologies

Here is the code … and it works perfectly well!

#define sp Serial.print

#define spln Serial.println

#include <SPI.h>
#include <Ethernet.h>

void setup() {
  Serial.begin(38400);
  IPAddress myIP=getIP();
  sp("returned ip is ");spln(myIP);
}

void loop(){;}

IPAddress getIP(){
  byte byteArray[4]={0,1,2,3};
  IPAddress ip (byteArray[0], byteArray[1], byteArray[2], byteArray[3]);
  sp("ip in callee is "); spln(ip);
  return ip;
}

you code will work until it doesn’t.

the object “ip” becomes undefined once you exist the function.

arduino_new:
you code will work until it doesn't.

the object "ip" becomes undefined once you exist the function.

But the value is returned by the function.

arduino_new:
you code will work until it doesn't.

the object "ip" becomes undefined once you exist the function.

Thanks. Must I understand that it worked by chance and that it may -depending upon the execution time? stop working?

Can you recommend a more robust implementation of this function?

guy_c:
Thanks. Must I understand that it worked by chance and that it may -depending upon the execution time? stop working?

I think that @arduino_new is confused - if you had returned a pointer to "ip", then you would have had a problem, because the pointer would be to a variable on the stack that could be overwritten at any time after the return from the function.

But you're returning the variable itself, so that's OK.

Thanks TolpuddleSartre, Maybe you'll want to comment on the 'public IP updater' just posted here

TolpuddleSartre:
I think that @arduino_new is confused - if you had returned a pointer to "ip", then you would have had a problem, because the pointer would be to a variable on the stack that could be overwritten at any time after the return from the function.

But you're returning the variable itself, so that's OK.

I think you have it flipped. it is OK if it is dynamically allocated.

arduino_new:
I think you have it flipped. it is OK if it is dynamically allocated.

Huh?

TolpuddleSartre:
Huh?

Isn't "ip" a reference to an IPAdress object which was created inside the function?

No, see this :smiley:

"getIP()" makes no sense, since it would only make sense to call it once, thus it is not needed. In order to do what you want, you should pass the instance of "IPAddress" as an argument to "getIP" or return the numeric value representing the IP:

//As argument
void getIP1(IPAddress &ipa)
{
  byte byteArray[4]={0,1,2,3};
  ipa = IPAddress(byteArray[0], byteArray[1], byteArray[2], byteArray[3]);
  sp("ip in callee is "); spln(ipa);
}

//Numeric value
uint32_t getIP2(){
  byte byteArray[4]={4,5,6,7};
  IPAddress ip (byteArray[0], byteArray[1], byteArray[2], byteArray[3]);
  sp("ip in callee is "); spln(ip);
  return ip;
}

void setup() {
  Serial.begin(38400);
  IPAddress myIP;
  getIP1(myIP);
  sp("returned ip is ");spln(myIP);
  myIP = getIP2();
  sp("returned ip is ");spln(myIP);
}

Just in case some official, long-established, real-world examples of returning an IPAddress from a function are needed: