What happens when you call a Function?

This article explains what happens behind the scenes when a function is called and how to write code with the optimal number of functions.

This article probably has the most verbose article title, but this is the best title it could have. It clearly declares what this article is about, the work the compiler performs when you do something basic as to call a function. Besides it’s just a one-line thing in my code so what could possibly happen. But for the compiler, it is so much more. Let’s see what happens when you call a Function?

Functions

Functions are unavoidable when you write code, it is a go to for every programmer and in most functional programming language like Haskell, erlang, where functions are first class citizens, it is impossible to avoid function. But what happens when you call that function you have written somewhere in your code to do something special?

For clarity’s sake, this is what I mean by call a function.

function logger(text) {
    console.log(text);
}

function multiply(a, b) {
    logger("I will be logged");  // The function logger is called here
    return a * b;
}

In this above code, logger is being called in the multiply function. But what happens when the compiler/interpreter gets to the logger function? Let’s see what happens when you call a function.

What happens when you call a function? — The compiler

Every time you call a function from your code, the compiler generates a call instruction. Meaning; in a running program, in order to call a function, the compiler needs to create an entire stack frame for that function, which means that the compiler has to push certain information about that function onto the stack which are:

  1. the function parameters (if the function / method contains parameters),

  2. the return memory address.

Once the compiler returns the address of this function in memory, the compiler jumps to a different part of our binary code in order to start executing the instructions from the functions pushed to the stack. Then the return of the function has to be gotten and “returned” to the line of the code before the function is being called. This is just for one function, imagine doing this same thing for 40 or 200 functions. It gets crazy during compilation.

There basically is a lot of back and forth in memory in order to execute function instructions during function calls, and this back and forth takes time for the compiler to perform.

This back and forth can cause our programs to be slow if we have many deeply nested functions, or if we write code with functions that can be combined into one. This statement is very important and is what is most required to think of when writing code. You constantly need to check if a function is needed before creating it, especially when working in a team.

When to create a function

The best time to create a function is when you write repetitive code block logic in your code. That’s it. That’s the primary point of functions and that should be your primary point for creating functions when you code.

👉🏽 Remember, the primary reason for functions is to prevent code duplication.

Conclusion

With the knowledge from this article, writing functions in our code requires thinking of how to optimize the functions as much as possible and how to not bloat the code with too many functions. In my experience, it takes a level of expertise and experience to accurately know when a function is not needed. In most cases, it is just a case of going through the code over and over and over and over to optimize your code which will involve removing redundant functions, especially if you are working on a large codebase. I mean the enterprise kind of large codebase.

So now that you know what the compiler goes through to execute your function call. You know better than leave redundant functions in your codebase or functions that are called once which can be written inline. You also know better than to write functions for every logic that does not need a function.

I believe this knowledge will help you write better code as a developer.

You can check out my other articles about C++, Rust, Web3 and other programming and technical concepts.

Thanks for reading. 🙂