27 January 2015

Functions in C++ 3

This function adds up the digits in a number and returns the sum of them.



Line 3 : int digit_sum(int number) tells you that there is a function called digit_sum that returns an int and takes in an int that the function will call number

Line 5 : Declares an int called sum and initializes it to be 0.

Line 6 : while (number > 0) is a while loop that will iterate if number is greater than 0. 

Line 7 : sum += number % 10 sets sum equal to itself plus the remainder of number divided by 10. 

Line 8 : Sets number equal to itself divided by 10. Remember, this is how many times 10 can go in to number fully. 

Line 10 : Closing bracket for the while loop.

Line 11 : return sum returns the value currently assigned to sum as the int return value the function must return. 

Line 12 : Closing bracket for the function. 



Farewell pic :

Check out this gutsy parking job that occurred outside my house. 


                                                                                                     ~ Cici


No comments:

Post a Comment