## Functions in math vs programming

My coffee thought for this morning is that the fundamental concept of function in your calculus class is the same idea as a function in computer science. Basically, a function is a way of indicating a formula that can take input and produce output. To illustrate, here’s a basic example of a mathematical function:

f(x)=x3

This formula takes in a value for x and returns the cube value of x. If you feed this function a value of 2, it’ll give you back a value of 8. Give it a 5 and it’ll return 125.

Here’s a function I wrote in the Kotlin programming language called `cube()` that performs a similar (for lack of a better word) function:

``````fun main() {
println(cube(2))
}

fun cube(number: Int): Int {
return number * number * number
}``````

This function takes an integer as input and will return the cube value of that number, just like the mathematical formula I mentioned earlier. The results are the same: Feed this function a value of 2, it’ll give you back a value of 8. Give it a 5 and it’ll return 125.

## Vive la différence!

It is worth noting one major difference between math functions and code functions: The math function will work for any value, while the computer function has limitations on the size of the data type. For the above example, we could increase the size of the data type by using `Long` instead of `Int`, but either way there’s an upper limit as to how big of a number you can use. Incidentally, in Kotlin the `Int` the upper value limit is 2147483647, and for `Long` the upper limit is 9223372036854775807. In Kotlin, you can use `MAX_VALUE` to find the upper limit of a given data type, such as `Integer.MAX_VALUE` and `Long.MAX_VALUE`.

Code functions can also do lots of other things besides return a value. You can throw in print statements, additional routines, more functions, and all sorts of other side effects. A math function does exactly the same thing every time given similar inputs, while a code function can behave differently depending on the state of the program when the function is called.

But overall, math and code functions can generally be thought of as the same beast. I found this to be an interesting thought exercise to consider the similarities and differences between the domains of calculus and computer science.

## Powers that be

While going through this thought exercise, I decided to segue my thought process into how Kotlin handles exponents. Here’s a Kotlin function I wrote called `powerOf()` that takes a number and a power value and returns the result:

``````fun main() {
println(powerOf(2.0,3.0))
}

fun powerOf(number:Double, power: Double): Double {
var count = power
var result = 1.0

while (count != 0.0) {
result *= number
--count
}
return result
}``````

Here I use a `Double` data type to handle any double-type inputs or results, and the invocation of `powerOf(2,3)` in `main()` results in 8.

However, I didn’t really need to write any of this, except to satisfy my morning coffee curiosity to work out how such a function might be implemented. You can more easily just import `kotlin.math.pow` to get the same result with much less code like this:

``````import kotlin.math.pow

fun main() {
println(2.0.pow(3.0))
}``````

I realize that was an unnecessary side trip, considering the title of this post was about the differences between math and code functions, but this darn keyboard lets me type anything…