The basics of programming: Functions

Octavio Gonzalez-Lugo
2 min readMar 15, 2023

--

Gabriel Heinzer at Unsplash

Knowing how to make the same operation multiple times and how to adapt the code behavior due to the flow of information can help you to develop a wide range of applications. However, when the calculations or the intended purpose of the code gets complicated a good idea is to compartmentalize the operations. One of the simplest forms of compartmentalizing operations inside a programming language is by using functions.

Functions allow you to compartmentalize the operations inside a block of code. Inside the such block, all the variables are local, meaning that only the functions can access those values. Outside the block of code, the variables do not exist. Preventing the duplication of variables that could lead to unexpected behaviors

Another variation of functions is the one-line function. Such functions can be declared on a single line of code. In python for example those functions are called lambda functions, while in javascript are arrow functions. The short syntax of those functions allows for the addition of complex behavior with little code.

Functions can be used to isolate behavior and remove repetitive instructions inside the code. Functions can also be combined with the other different functions inside the programming language or other user-defined functions.

The combination of the IF/else function, the for loop, and the declaration of custom functions create a powerful combination to develop code. Function composition can also help to reduce the size and complexity of the code by dividing complex calculations into many composing functions. This will allow each function to perform only one task and hopefully reduce the number of bugs.

Information flow management, repetitive operations, and compartmentalization are the fundamental components of code. Although there are specific functions within the different languages, most of the different operations can be reduced to one of those functions or a combination of them.

Which programming language is the best to start I think is a personal decision. If you want to start to develop code the best approach, at least in my opinion, is to start with different languages and stick with the one you find the easiest to follow. Then when you need to learn a new language it will be easier to move from one language to another. Also if you already have some small applications under your belt, you can try to reimplement them under the new language. As you already know the behavior of the code you will know what to expect with the new implementation.

--

--

Octavio Gonzalez-Lugo

Writing about math, natural sciences, academia and any other thing that I can think about.