Course Introduction to Programming

Modularization

Course's Page 10 min Text by
User Image
Rahilly Machado

In this class, we will learn about one of the fundamental concepts of programming: Program Modularization.

We will understand what it means to modularize, what its main advantages are, see practical examples of how to divide a program into functions, and finally, comprehend how these functions allow us to organize code, facilitate maintenance, and create clearer and more reusable programs.

What is modularizing?

Modularizing means dividing your program into modules, so each module will perform a specific task. Until now, all our programs have been executed in a single module: the main function, or the main function, which contained everything we wanted to execute.

Why modularize?

Modularizing a program makes the programmer code small tasks at a time. This will:

  1. Facilitate debugging: locating and correcting errors in small parts of the code is much simpler than in a large, monolithic program.
  2. Improve readability: each function has a specific task, so it becomes easier to understand what the program is doing.
  3. Eliminate redundancy: instead of repeating the same piece of code multiple times, we can call the function as many times as necessary.

If you don't yet know what these concepts mean, here is a brief description of each of them:

  • Debug: the process of locating and reducing errors in the code. This way, it is easier to find and correct errors in code that is modularized into small tasks than in non-partitioned code.
  • Readability: the ease of understanding what the code is doing. With this in mind, it is easier to comprehend the functionality of code modularized into small tasks than that of non-partitioned code.
  • Redundancy: using the same piece of code more than once in the same program. In modularized code, you could call the same task numerous times without having to rewrite it.

What is a function?

To modularize a program, we will divide it into functions. A function is, in short, a block of code that receives certain parameters, processes them, and returns some value.

Examples:

Functions in C

To create a function in C, we must specify:

  1. Return type: the data type that the function will return;
  2. Name: the name we will use to call the function;
  3. Parameters: the data the function needs to work;
  4. Code: the function's code.

The creation of a function in C follows the template below:

<return type> <name> ( <parameters> ){
    <code>
}

โš ๏ธ Note: the parameters must have their types identified and, if there is more than one, be separated by commas.

Coding

Below is a code that defines and uses a function with no parameters that just prints a sentence:

โš ๏ธ NOTE: When the function returns nothing, the correct return type is void. But don't worry about function types now, they will all be explained in a future class ๐Ÿ˜‰

#include <stdio.h>

/*
    Return type: void (returns nothing)
    Name: imprime
    Parameters: none
    Code: prints a sentence
*/

void imprime(){
    printf("Neps Academy\n");
}

int main(){
    /* calls the "imprime" function, providing the necessary
    parameters for it inside the parentheses (which, in this case, there are none)
    */
    imprime();
    imprime(); // we can call the same function multiple times
}
/* Output:
Neps Academy
Neps Academy

*/

Advantages of using functions

Using functions brings advantages to the code, among them:

  1. Reusability: the same function can be called multiple times in different parts of the program.
  2. Organization: each function handles a single task, keeping the code modularized.
  3. Ease of maintenance: changing a function affects only that module, without impacting the rest of the program.
  4. Readability: makes it easier to understand what each part of the program does.

Conclusion

Throughout this class, you learned what modularization is, what its advantages are, and how it helps to organize, simplify, and reuse code. You also understood the concept of a function, saw how to create functions in C, and their importance.

Now you know how to divide a program into functions, call them inside main, and create more organized, readable, and efficient solutions ๐Ÿ˜ƒ