terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat logica-funciones.md
guest@codeandosimple: ~/blog/logic $ cat functions.md

Logic - Functions_

// "The way to get started is to quit talking and begin doing" - Walt Disney

In this article we will see functions (and also procedures). These components are the fundamental bricks for building scalable and maintainable software.

Video thumbnail

# Divide and Conquer (functions)

With this phrase "divide and conquer", attributed to Julius Caesar, the Romans found an extremely effective strategy for politics and war.

This is an idea that also applies to problem solving. Given a complex problem, we divide it into parts or subproblems, which are simpler to solve.

Flowchart: Input, Process and Output in modular functions

Once the solution for each of the parts or subproblems is found, we combine them and solve the main problem.

Hierarchical structure of modularization in algorithms

We call each part a module, and the entire process, modularization. Each module solves some of the subproblems that make up the original problem.

info Technical Note: Modularity and Coupling

Modularization not only simplifies logic, but also allows managing coupling (the dependency between modules) and cohesion (how focused a function is on a single task). A good modular design reduces the technical debt of the project.

# Functions

Functions are blocks of code that perform specific tasks. They divide large tasks into several smaller tasks, and allow reusing what we already did, or even what others did, instead of starting from scratch.

# Structure of Functions

A function has the form:

Technical syntax of a function: Return type, name and arguments

The name of the function, the data type it returns and between parentheses the parameters, separated with ",", each with its type.

Finally, it returns the result to the caller.

# Parameters and Variables

Parameters are variables used to put data inside the function, because the function doesn't know what's outside; it only knows what is declared inside and the parameters.

return_type function_name()

? Global variables

There are variables that are declared outside, called global variables, which can be used anywhere in the program. In programming, it is recommended not to use, or use as few global variables as possible to avoid unexpected side effects.

# Procedures

Some functions do not return a value; in that case, the return type is void. They are called procedures; they take care of some task but return nothing.

void function_name (params)

# Example: Sum Function

Sum function code

We need to build the main program, which loads the data, calls the sum function and prints the result.

Main program code

# Parameter Passing

There are two types of parameter passing: Value and Reference.

By Value

Passing by value

// Copy of the data, does not affect original.

By Reference

Passing by reference

// Memory address, modifies original.

# Practice Exercises

Resources used