Arduino Forum

Using Arduino => Programming Questions => Topic started by: tgross on Apr 05, 2011, 09:44 pm

Title: Using "return" and its associated syntax
Post by: tgross on Apr 05, 2011, 09:44 pm
I'm using a ds1307 chip to bring real time into my Ard Pro Mini and someone posted some code out here in 2008 that looks like what I want.  However I don't exactly understand the syntax that's being used here.   My problem is that I do need to change hex values from the ds1307 to dec values and I'm having a problem doing this so far.   This is the code that I think I need to utilize but I don't exactly understand it's syntax:


int DS1305::bcdToDec(const byte bcd) {
      return ((bcd / 16 * 10) + (bcd % 16));
}

This was posted by windmis in 2008.  My questions are as follows:

On this code what's going on with the double colon, "::" ?   
After byte there appears to be a function name and then a variable or vice versa, how does this work? ie, "byte DS1305::bcdToDec".
After the function name there are three variables in the parenthesis,  "(const byte bcd)" .   I guess this is a variable, datatype, variable ?   
.
Your help would be appreciated.  Thank you,
.
Tim
Title: Re: Using "return" and its associated syntax
Post by: Korman on Apr 05, 2011, 09:49 pm
The :: indicates that you have a method bcdToDec of the class DS1305. If you have an object of this class, you can access this functions as:

Code: [Select]
DS1305 foo;
...
int baz = foo.bcdToDec (35);


The "const byte bcd" just means there's one parameter called bcd of type byte which isn't expected to changed inside the function (the const part). The last const is mostly a hint for the optimiser so that it knows it can do nasty things without having to worry about restoring the original value.

Korman
Title: Re: Using "return" and its associated syntax
Post by: robtillaart on Apr 05, 2011, 09:58 pm
Quote
On this code what's going on with the double colon, "::" ?  


int DS1305::bcdToDec(const byte bcd)

This means this is a function with the name  "bcdToDec"
+ which is part of the class named "DS1305"  
+ needing a parameter named "bcd" of the type const byte
+ and this function retuns an int.

If you want to have three parameters you need to separate them with ,  e.g  int max(int x, int y) ....

more see - http://www.arduino.cc/en/Reference/FunctionDeclaration

Title: Re: Using "return" and its associated syntax
Post by: tgross on Apr 06, 2011, 12:12 am
robtillaart and Korman thank you for your advice.   This will certainly help.   And the reference to the function syntax was a magic bullet.    Thanks guys!
.
Tim