Language: EN

programacion-que-es-una-biblioteca

What is and how to use a library

A library is a set of pre-written code that you keep together so that you can use it and reuse it in your program.

Imagine having to rewrite a function from scratch every time you wanted to use it. Well… programming would be a very tough exercise of reinventing the wheel over and over again.

To avoid this, there are libraries. They are simply collections of code snippets that we store grouped together so that you can reuse them. This way, you save time by not having to recreate them.

programacion-que-es-una-biblioteca

Libraries are called that because they are like a “place” where you store your code snippets, well organized, like you do with books in a library.

Libraries are often called “libraries” because of the similarity to “library” in English. It is actually a misnomer and there are very radical people about this. But don’t feel too bad about it, it happens to all of us.

If you prefer, you can think of them as a toolbox where you store useful codes, whether they are yours or from other people. Then you can open the box, take out the tools you need, and use them whenever you want.

Libraries can be:

  • Internal, written by yourself or a coworker
  • Open Source, open source written by the community
  • Third-party, usually paid

When you want to use a library, you must first incorporate it into your project. Then, you can call the functions and use the objects and classes that have been defined in the library to perform specific tasks in your program.

In fact, it is currently impossible to write a program without using libraries. You will always use them, even if they are from the language itself, or even from the operating system.

Examples of libraries in different languages

Suppose we have a library with mathematical functions, which are very useful to you. For this example, let’s assume that they are functions that you have created yourself and, logically, you want to be able to reuse them in your programs.

Let’s keep the example simple, so we will only have two functions in your super math library.

  • Add
  • Subtract

Let’s see how we could define your library of mathematical functions in different programming languages.

Creating a library in C# is very easy thanks to the use of Assemblies and namespaces. For example, we create a new file that contains the namespace MyMathLibrary.

Inside this, we would create a public class that will contain the sum and subtract functions. You can call it, for example, MathOperations.

using System;

namespace MyMathLibrary
{
    public class MathOperations
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }

        public static int Subtract(int a, int b)
        {
            return a - b;
        }
    }
}

Now, from our program we can import our library using the word using

using System;
using MyMathLibrary;

namespace MyProject
{
    class Program
    {
        static void Main()
        {
            int sumResult = MathOperations.Add(5, 3);
            int subtractResult = MathOperations.Subtract(10, 6);

            Console.WriteLine("Sum result: " + sumResult);
            Console.WriteLine("Subtract result: " + subtractResult);
        }
    }
}

In C++ we can organize functions into libraries using header (.h) and implementation (.cpp) files.

For example, we would create a header file called math_operations.h

#ifndef MATH_OPERATIONS_H
#define MATH_OPERATIONS_H

// Function to add two integers
int add(int a, int b);

// Function to subtract two integers
int subtract(int a, int b);
#endif

And an implementation file math_operations.cpp that would implement the functions

// math_operations.cpp

#include "math_operations.h"

// Implementation of the function to add
int add(int a, int b) {
    return a + b;
}

// Implementation of the function to subtract
int subtract(int a, int b) {
    return a - b;
}

Now, from our file we could include our library of math functions by adding the header file math_operations.h

// main.cpp

#include <iostream>
#include "math_operations.h"

int main() {
    // Example of using the functions
    int sum_result = add(5, 3);
    int subtract_result = subtract(10, 6);

    std::cout << "Sum result: " << sum_result << std::endl;
    std::cout << "Subtract result: " << subtract_result << std::endl;

    return 0;
}

In JavaScript we can create modules, provided by the ECMAScript (ES6) standard, using the export and import keywords.

For example, we can create a file called mathOperations.js

// Function to add two integers
export function add(a, b) {
  return a + b;
}

// Function to subtract two integers
export function subtract(a, b) {
  return a - b;
}

Then, in another JavaScript file, we could import the module and use its functions

// Import the functions from the mathOperations.js module
import { add, subtract } from './mathOperations.js';

// Example of using the functions
const sumResult = add(5, 3);
const subtractResult = subtract(10, 6);

console.log("Sum result: " + sumResult);
console.log("Subtract result: " + subtractResult);

In Python we can also organize functions into modules. For example, we would create a file called math_operations.py

# Function to add two integers
def add(a, b):
    return a + b

# Function to subtract two integers
def subtract(a, b):
    return a - b

Then, in another Python file, we could import the functions with import

# Import the functions from the math_operations.py module
from math_operations import add, subtract

# Example of using the functions
sum_result = add(5, 3)
subtract_result = subtract(10, 6)

print("Sum result:", sum_result)
print("Subtract result:", subtract_result)

For it to work, it is only necessary that both files are in the same directory (or in a directory that Python can find).

If you got a little lost in any of the examples, don’t worry. The syntax of each language in this post doesn’t really matter. You will see the details when you study each of the languages.

What is important is that we see that practically all languages allow us to create libraries and import them into other projects to use the functions.