
I remember, I saw this image around 3 years ago, when I myself was starting to learn programming and I also thought that maybe I don't need to learn mathematics or the concepts in mathematics, to be a good programmer. To an extent, I was right, to an extent I was wrong. And this post is entirely about the concept of why I was wrong and why I was right.
Also, before the post even starts, I want to be the "mainstream author" and start my own quote.
In software, everything is a statement, unless it is a function.
Mathematical definition of a function
The simplest of the definitions of a function, define it as a function has,
- A set of inputs, which is known as argument (or argument list).
- A value that is returned from the function.
For a function, f(x, y) → y ÷ x,
- The domain of the function would be the set of all the possible values of x and y, where the function doesn't break—I will talk about this part in the code below.
- The codomain part of this function, is the set of possible values that the expression "y ÷ x" would return.
- double Function(int x, int y) {
- return y / x;
- }
- double Function(int x, int y) {
- if (x != 0) {
- return y / x;
- } else {
- // Properly propagate the value
- return Double.PositiveInfinity;
- }
- }
When is a Function, a Function
An object has a state and performs some functionality.
Fine, then what is the functionality when it could be much better explained as a behavior? Wouldn't it be easy to understand? But still most of the course outlines, materials do not focus on these ambiguities that, for a moment do seem to help the reader, but can be a long-term pain for the readers as they would sometime stumble upon the real usage of world and then, cringe. So, how do we then define a function?
- int SomeName(int param1, int param2) {
- // Processing
- // More Processing
- return something;
- }
- Is stateless — whereas in the object-oriented programming languages, we have seen that a function can be stateful.
- Should have no side effects, I am unsure whether you would consider output stream writing a side-effect or not, so I would leave this.
- Lazy evaluation, and other features that are typically applied to functional programming may or may not be included and involved.
Stateless
First of all, you should understand what a state is. A state (I won't talk about the object-oriented concepts, so stick with me) is any external value that might cause a change in the output value of the function. For a function to be stateless, it must return the value by processing the inputs only, and not any other state or value should alter the output value. For example,
- Sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
No side effects
Side effects are, opposite to states, as in the case of a state an external value was altering the result, in the side effects case an external value is altered by the input. For example take the following code as the example,
- // void set intentionally
- void someFunction(int a, int b, int c) {
- // Stateless
- int d = Sum(a, b, c);
- // Checking a condition
- if(d > 100) {
- // Side effect!
- salesMet = true;
- }
- }
Lazy evaluation
This part, is all left for the programmers, the only thing is, that a function should be evaluated only when needed. You should read more on this yourself, sorry.
So, what is a Function in Imperative (Procedural) languages?
Now comes the final part, what is the function that we have defined in object-oriented programming languages? I remember, my teacher, Sir Zeeshan, while teaching us Visual Programming last semester, asked the students to not call the functions, "functions", but instead use the name "methods". Despite all the problems that there were in the class, I personally loved that statement made by him because he was trying to teach the students something "real" and not what they would more likely find in the books.
Another way to look at the functions in imperative languages, or in computer science is, that we typically are based on the sequential codes. Everything about a computer, gets boiled down to 0-1 in the binary. Thus, the computer has to follow the sequence of codes, more friendly languages such as C, C++ or Java and C# are providing a good "experience" for the developers, to write the software code. "Functions" in these languages, is much like a structure to wrap a code with. Thus, being a simple area or buffer of code statements, that gets called by the runtime — or whatever calls it, you get the point, right? In such cases, a function would typically only be a labelled area of code statements — and now would be a good time for you to go back and see the initial quote that I started, right away — and, if there is a response, it gets pushed on stack, otherwise the control returns.
- void Function(int x, int y) {
- int c = y / x;
- }
- Function:
- IL_0000: nop
- IL_0001: ldarg.2
- IL_0002: ldarg.1
- IL_0003: div
- IL_0004: stloc.0 // c
- IL_0005: ret
The answer is pretty much simple, and straight-forward, since the functions in these languages are nothing but just wrappers around a block of code statements, that is why they are not required to return anything at all. If they, however, do return something, it gets pushed to the stack and the caller statement has to load that from stack. And that is exactly, why they are called "methods" and not the functions. Calling the behavior of the objects, a function would be confusing and misguiding, however the concept and title of "methods" is much simpler and less confusing once a person starts to dig deeper in Mathematics of this as well.
- A full support and allowance of return-or-not-return, take-parameter-or-not-take-parameter behavior.
- Stateful, as needed.
- Side effects, if medicines can have them, why can't the methods have side effects too?
- However, they still might have a domain and a codomain on which they operate and definitely would break in curtain conditions and situations.
Functional language functions (and difference)
Functional languages, are typically the languages that support recursion, and thus require functions to be there. Haskell for example, is an example of pure functional programming language, it does not contain impurities such as object-oriented concepts. Pulling some help from this thread on Stack Overflow, the functions in Haskell, cannot return void. They must return something, and must always take a parameter. Obviously the syntax of the functions is really evaluated to be short and precise, but underground concept is all same. Also, to note that functional programming language, Haskell, does support some simple actions, where the structure does not take a parameter and returns nothing
Note
Why can't it? It is written on top of C though.
Finally: Lambdas, and their story
Lambdas, lambdas, lambdas, C++, C# and almost every functional programming language has these features at its core. Lambda functions are just so much useful, interesting to learn, easier to code and beautiful in C# that you just cannot skip them and talk about functions, just no.
The story of lambdas started with Alonzo Church, who created them for his own work in the field of "Mathematics". Notice how important the lambdas are in the computer science, and they were created for Mathematics, and not for other stuff. The core concept of lambdas is that the functions, whatever they do, are actually anonymous mappers, that map an input to an output. What lambdas bring to computer science is nothing but a syntax, that makes the functions cleaner, and a bit of overhead removal from the function itself.
- They are stateless, they operate directly on the operands, or the input values.
- They are not meant to have any side effects. Although in the cases of, say C#, when you create delegates and event handlers, there are side effects in the lambdas, anonymous function. But that is discourage.
- They directly operate on the operands, thus generating the same result everytime that they are called.
And finally, I hope this article might have provided you with some useful insights, and tips and understanding of what a function is what the naming conventions are. Typically nowadays, it doesn't matter what you call what, the only thing that matters is, does your code compile?

Join the conversation! Your thoughts help the community grow.