# Modularization

:::{wpd} modular programming
a programming paradigm that emphasizes organizing the functions of a codebase into independent modules – each providing an aspect of a computer program without providing other aspects.
:::

## Defining functions

```c
int square(int n);  // Function prototype (typically in .h files)

int square(int n) {  // Function definition (typically in .c files)
  return n * n;
}
```

## Managing multiple modules in C

```text
📁 include/   <= How is a module used?
  📄 lib1.h
  📄 lib2.h
📁 src/       <= Implementations
  📄 lib1.c
  📄 lib2.c
  📄 main.c
```

## VS Code considerations

You have to modify:
1. `compile_flags.txt`, so that language server can see the header files:

   ```text
   -std=gnu2y
   -g
   -Iinclude
   ```
1. `tasks.json`, e.g.,:

   ```json
   "command": "clang",
   "args": [
       "'@compile_flags.txt'",
       "./src/main.c",
       "./src/lib1.c",
       "./src/lib2.c",
       "-o",
       "main"
   ],
   ```
   Alternatively we can use a `makefile` and use `make` as the command.
