Go Down

Topic: need help explaining this (Read 146 times) previous topic - next topic

leon_ronin

hi. I'm currently working on an Internet clock which connects to NTP server and reads the time. I'm using adafruits code as reference but I have some problems understanding the code. here is the first part:
Code: [Select]
unsigned long sendNTPpacket(IPAddress& address)
why is there a "&" at the end of "IPAddress"? is this always necessary?
 my second question is about a line of the code which sets the time zone offset in relation to UTC:
Code: [Select]
int tzOffset = -4;                  // Time zone offset 
what if my time zone offset is like say UTC+5:30?

MarkT

Thats the syntax for pass-by-reference.

For finer tz granularity you could rework tzOffset to be in minutes, and change all the uses of it.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

leon_ronin

Thats the syntax for pass-by-reference.
now what's that???

pert

now what's that???
I think it is well explained by the "Arguments passed by value and by reference" and "Efficiency considerations and const references" sections of this page:
http://www.cplusplus.com/doc/tutorial/functions/
In this case, it is done for the sake of efficiency.

Note this code does not follow best practices. They should have made the address parameter const. If there was an error in the code that inadvertently modified the value of address, this could result in a very confusing bug with the current code. But with const you would get a helpful compiler error message and be able to immediately spot the problem. In fact you should make every variable const if you don't intend to change the value, it's just that this is even more important in the case of pass by reference.

Delta_G

now what's that???
It's a term you can google and get tons of info about. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Robin2

Code: [Select]
unsigned long sendNTPpacket(IPAddress& address)
why is there a "&" at the end of "IPAddress"? is this always necessary?
I think I am correct in saying that & is between the IPAddress and address - in  other words these versions would have exact same effect
Code: [Select]
unsigned long sendNTPpacket(IPAddress & address)
unsigned long sendNTPpacket(IPAddress &address)
unsigned long sendNTPpacket(IPAddress&address)


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

Yes, & is a token, identifiers are tokens, whitespace is optional between tokens _unless_ it would
be ambiguous w.r.t. a longer token.  Thus one needs a space before two else it
would be a single token onetwo, += is always a single token, +  = is two tokens
and I believe is is syntactically impossible in a valid program.

So you are right all three are exactly the same to the compiler.

Passing by reference is a choice you make.  Its usually preferable to passing an explicit pointer,
which can be used to achieve the same effect.  Passing by value and pass by reference are worth
getting straight in your head, side effects on something passed by reference are visible outside the function.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up