A library is a set of already written code, that you keep all together so that 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, there are libraries. They are simply collections of code snippets that we keep grouped so you can reuse them. (this way you save the time of recreating them).
Libraries are called that because they are like a “place” where you keep your code snippets, nicely organized, just like you do with books in a library.
Libraries are often called librerías, due to the similarity with “library” in English. It’s actually incorrect and there are very radical people about this. But don’t worry too much about it, it happens to all of us.
If you prefer, you can think of them as a toolbox where you keep useful codes (whether they are yours or from others). 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, written by the community
- Third-party, usually paid
When you want to use a library, you first need to 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, currently it would be impossible to create a program without using libraries. You are always going to use libraries. (even if they are the ones from the language itself, or even from the operating system).
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 that they are functions you’ve created and logically, you want to reuse them in your programs).
Let’s keep the example simple, so we will only have two functions in your super mathematical 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 simple thanks to the use of Assemblies and namespaces. For example, we create a new file containing the namespace MyMathematicalLibrary
.
Within this, 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;
}
}
}
Now, from our program, we can import our library using the using
keyword.
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);
}
}
}
In C++, we can organize functions into libraries using header files (.h) and implementation files (.cpp).
For example, we would create a header file named 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
And a file mathematical_operations.cpp
that implements 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;
}
Now, from our file we could include our mathematical functions library 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;
}
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 named 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;
}
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);
In Python, we can also organize functions into modules. For example, we would create a file named 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
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)
For it to work, both files just need to be 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 entry, doesn’t matter much. You will see the details when you study each of the languages.
What is important is that we see that practically all languages allow you to create libraries and import them into other projects to use the functions.