When one learns their first programming language and reaches a chapter titled functions, quite scary (or not so much) images from math classes may pop up in one's head in a form of curves on graphs or f(x)-notation expressions. By the end on the chapter none of those things are encountered (phew) and the knowledge remained afterwards basically is: if you have a bunch of lines of code in your program which you predict to call more than once - consider wrapping it into a function and reuse.
Seems like it doesn't fully match with what we studied about functions at school, does it?
Functions in mathematics
When people work with sets (collections of objects) they often need to define relations between members of given sets. The term function may be used to represent how one quantity depends on another. Or to show an assignment of one element of codomain Y to each element of domain X. Or it is a "rule" which maps elements from one set to elements from another set. Or, in other words, function is something that returns an output for each provided input. The key words here are sets and mapping.
You may recall a quadratic function f(x) = ax² + bx + c (where a, b, c are constants and a is not 0) and its graphical representation (parabola):
There is still a question why I needed this quadratic relations knowledge at school, but here are a few more questions one may have after reading the given definitions:
Are there functions without domains (arguments)?
Yes, they are called constants (e.g., f(x) = 1; domain x is still a part of a function definition though, but it does not affect the result).Are there functions without codomains (return value)?
Yes, but a function with an empty codomain can only exist if its domain is also empty. Scenario with nonempty input and empty output is impossible!Are there functions with multiple domains?
Sure, they are called multivariable functions (e.g., adding numbers).Are there functions which do not have outputs for every possible input?
Yes, they are called partial functions (e.g., division by zero is undefined for real numbers).Are there functions with side-effects?
No, in mathematics functions are side-effects free!
It is a pretty general overview, there are of course a lot of other types and categories of functions. Let's now take a look at functions programmers write and how they are different (or not) from the definitions above.
Functions in computer science
In programming functions are heavily inspired by and derived from mathematical functions, and they may or may not follow its principles. Consider the code below:
function fairCoffeePriceUSD(coffee: Coffee): number {
switch(coffee) {
case: Coffee.V60
return 1;
case: Coffee.BatchBrew
return 0.5;
default:
return 0.2
}
}
function processCoffeePayment(coffee: Coffee, card: Card): void {
const price = fairCoffeePriceUSD(coffee);
const res = card.makePayment(price);
console.log('payment result: ', res);
}
The first fairCoffeePriceUSD function maps coffee domain (enum Coffee) to prices codomain (type number) and nothing more.
The second processCoffeePayment function on the other hand breaks 2 important (mathematical) rules: it does not return any value accepting 2 arguments and it produces 2 side effects!
Outro
It is only one example but it suffices to conclude that computer science borrowed the name function (early computers were used primarily for scientific calculation) and extended its meaning to meet modern world needs (I can hardly imagine a side-effects free program).
Even though not all programming languages have this "misleading" function/func/fn keyword (most C family ones just use a return type before a name, others use def/define/defn), they still call it functions (methods in classes is an exception).
Functional programmers tend to call such non-pure functions procedures or subroutines though, try to stay true to mathematical principles of functions and handle effects on a "thin" layer outside, but this is another story...

Top comments (0)