# please explain this byte decToBcd(byte val) { return ((val/10*16) + (val%10));

Hi everyone
this is my first post so bear with me. hopefully i’m putting this in the right place.
so im trying to create a clock using the lcd that comes with the official arduino starter kit and the freetronics rtc module.

now i know there are probably simpler ways to do this and the circuit is not going to be left constructe very long, this is purely a learning exercise.

now i know there is a library available for the rtc but i want to do this without that (so i can learn). i read on the freetronics website about how to do it and i noticed this

``````byte decToBcd(byte val)
{
return ((val/10*16) + (val%10));
}
``````

can someone explain what is going on here? especially the first line? i can’t figure out where in the code to put it and i can’t figure out how it works.

i understand that the rtc works in BCD and i know what BCD is. the best i can figure is that this converts decimal to bcd but given that it seems to have something to do with this

``````Wire.write(decToBcd(second));
``````

im even more stumped. does this line of code tell it to run the number through the first set of code? can someone elaborate on what’s going on?

What you have there is called a “function”. It’s a small snippet of code that can be run from many other places just by using its name. It saves writing the same code out over and over and over again.

Just have done my first posting as well, but I should be able to answer this one:

``````byte decToBcd(byte val)
``````

defines a function.
You pass the value val to the function, the function does something with val and then returns a result
this example will make it easier to understand:

``````boolean isZero(int val){
boolean returnValue = false;
if (val == 0) returnValue = true;
return returnValue;
}
``````

So isZero returns either true or false, depending on the value of val.

Or as a small sketch:

``````void setup(){
for (int i = -5; i <5; i++){
System.print(i);
if (isZero(i))
System.println(" is Zero");
else
System.println(" is NOT zero");
}
}
void loop(){}

boolean isZero(int val){
boolean returnValue = false;
if (val == 0) returnValue = true;
return returnValue;
}
``````

sorry but i promise i've been digging around on the arduino reference page for a while trying to wrap my head around all this for a while now...

how do the to statements relate to each other in terms of variables? what does the bit at the end of the function title do? for example mine has "byte var" however in the call phase(is that the right terminology?) it is "decToBcd(second)". i figure "second" should be a number set else where but who doe it relate to "byte decToBcd(byte val)" also i noticed in the example there were 2 numbers instead of 1 does it work like this? dectobcd(x,y,z) becomes x,y,z in the function? and are independant of variables set in the rest of the code?

what does the bit at the end of the function title do?

It is called a formal parameter list. Or more commonly known as the function arguments or parameters.

It describes the type of data the function accepts as inputs. It can accept multiple inputs separated by a comma ','

Check out the cplusplus.com tutorials:
Functions - C++ Tutorials
C++ Language - C++ Tutorials

oooohhhhhhhhhh now that makes sense well mostly haha...

another question then.
i'm assuming this isn't like define which when the code is compiled goes through and replaces all the defined words with their values for example defing ledpin as 1 goes through and replces all occourences of ledpin with a 1 and not really shortening the code? im guess ing that a function only is stored to the chip once in a way and the program is diverted through it when necessary? (sorry if that doen't make sense but im ot sure of a better way to phrase that

also is this equation return ((val/10*16) + (val%10)) capable of converting DEC to BCD? if you make val=9 will the result be 1001 ? I can't figure out how to run it through a calculator to test it for myself...

((val/10*16) + (val%10))

lets take val = 25

val is an integer(whole number), so val /10 would give 2.
Multiply by 16 ---> 32

(val%10) is the remainder of val / 10 ----> 5

so your function would return 32+5, which is 37

If you convert this to a binary number, this would be

0010 0101
2 5 (Decimal)

so technically it doesn't return a BCD number, it returns a decimal number that when converted to binary will equal the BCD of the original number? i take it this is because when it get converted to be sent over i2c it goes to binary and the clock needs bcd. that makes sense... thanks for that.

I think part of your confusion is not understanding that a BCD (Binary Coded Decimal) number divides a memory byte into two 4-bit fields. Each of these 4-bit fields is called a nibble...honest! Each nibble is capable of holding a digit value 0-9. Now, go back and read bbacke's note and see if it doesn't make more sense.