Anything that would require using something multiple times, or it makes sense to seperate out the function for documentation reasons, should be a function. In this case, a function isn't necessary because you already have the '+', '-', '*' and '/' operators to do everything you need. However, if you had a formula or algorithm that you wanted to do for everything in say, an array, you should use a function there.
For example, you could write a program that reads in an array, and then have a function that works out the square root of every value in the array, by having a
sin function which you pass each element in the array to seperately. Then the program just adds the values together (or something) and prints them. Try that.
For your reference, sin is defined as:
sin x = x - (x^3)/3! + (x^5)/5! - (x^7)/7! + ... |
For this, you will need to do a power function as well (not too hard, just loop multiplying the number) and a factorial function, where n! is defined as
n! = n * (n-1)!; 1! = 1; 0! = 1 |
So you can use a recursive function (function that calls itself), but don't need to. Do a recursive function anyway, just because.
In case you are wondering why I picked this, I did it just then, and felt like giving it to you. Sorry its a bit hard!