In this article we will see functions (and also procedures). These components are the fundamental bricks for building scalable and maintainable software.
# 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.
Once the solution for each of the parts or subproblems is found, we combine them and solve the main problem.
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:
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
We need to build the main program, which loads the data, calls the sum function and prints the result.
# Parameter Passing
There are two types of parameter passing: Value and Reference.
By Value
// Copy of the data, does not affect original.
By Reference
// Memory address, modifies original.
# Practice Exercises
Create a converter function (currency, amount) for pounds, dollars, and yenes.
DECIMAL converter (STRING currency, DECIMAL amount)
START
DECIMAL conversionRate
IF (currency == "pound") THEN
conversionRate = 1.22
ELSE IF (currency == "dollar") THEN
conversionRate = 0.75
ELSE IF (currency == "yen") THEN
conversionRate = 0.009
ELSE
START
PRINT "Invalid currency"
RETURN -1
END
RETURN amount * conversionRate
END
Calculate the daily wage based on day/night shifts and weekend bonuses.
DECIMAL calculateWage (INTEGER hours, STRING shift, STRING day)
START
DECIMAL baseRate, increase, finalRate
IF (shift == "night") THEN
baseRate = 12; increase = 0.15
ELSE
baseRate = 10; increase = 0.10
IF (day == "saturday" OR day == "sunday") THEN
finalRate = baseRate + (baseRate * increase)
ELSE
finalRate = baseRate
RETURN hours * finalRate
END