programacion-que-es-una-biblioteca

What is and how to use a library

  • 6 min

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

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

To avoid this, libraries exist. They are simply collections of code snippets that we save grouped together so you can reuse them. (this way you save the time of creating them again).

programacion-que-es-una-biblioteca

A library is where you save your code

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

Libraries are often called “librerías” in Spanish, by similarity with “library” in English. Actually, it’s a misnomer and some people are very radical 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 code (whether it’s 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, first you must 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, nowadays it would be impossible to make a program without using libraries. You will always use libraries. (even if they are the language’s own, or even the operating system’s).

Examples of libraries in different languages

Let’s assume we have a library with mathematical functions, which are very useful for you (for this example, let’s assume they are functions you made 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 you 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 containing the namespace MyMathematicalLibrary.

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

using System;

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

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

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

using System;
using MyMathematicalLibrary;

namespace MyProject
{
    class Program
    {
        static void Main()
        {
            int resultAdd = MathematicalOperations.Add(5, 3);
            int resultSubtract = MathematicalOperations.Subtract(10, 6);

            Console.WriteLine("Result of addition: " + resultAdd);
            Console.WriteLine("Result of subtraction: " + resultSubtract);
        }
    }
}
Copied!

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

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

#ifndef MATHEMATICAL_OPERATIONS_H
#define MATHEMATICAL_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
Copied!

And a file mathematical_operations.cpp that would implement the functions

// mathematical_operations.cpp

#include "mathematical_operations.h"

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

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

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

// main.cpp

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

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

    std::cout << "Result of addition: " << result_add << std::endl;
    std::cout << "Result of subtraction: " << result_subtract << std::endl;

    return 0;
}

Copied!

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

For example, we can create a file called mathematicalOperations.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;
}
Copied!

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

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

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

console.log("Result of addition: " + resultAdd);
console.log("Result of subtraction: " + resultSubtract);
Copied!

In Python we can also organize functions into modules. For example, we would create a file called mathematical_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
Copied!

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

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

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

print("Result of addition:", result_add)
print("Result of subtraction:", result_subtract)
Copied!

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

If you got a bit lost in any of the examples, don’t worry. The syntax of each language, in this post, doesn’t matter much to us. You’ll see the details when you study each of the languages.

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