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 intro-functions.md
guest@codeandosimple: ~/intro $ cat intro-functions.md

Functions and Modules_

// "Patience is a key element of success" - Bill Gates

// The basics

Here we will talk about functions, one of the fundamental tools when writing our programs. We will see what they are and why they are so important.

Video thumbnail

# What are functions and what are they for?

Imagine a large program, like Word or Excel: they are millions of lines of code, impossible to handle if it is not organized in some way.

To solve these kinds of things we have something called functions. A function can be seen as a little block of code that does something, and follows the strategy of "divide and conquer".

divide and conquer

We have a big, difficult task. We can divide it into smaller, easier-to-solve tasks.

small tasks

We solve these tasks and unite that solution.

solution union

Our program is like this task: it is big, has many functionalities, it does many things; and when programming it we divide it into smaller parts, simpler functionalities, and then we invoke these solutions. Generally, our main program ends up having a few lines that invoke functions.

function invocation

We've been using functions but we haven't seen them: every program starts with a main function, for example Main.

main function

We also used primitives read, print, which are functions that communicate with the operating system.

OS primitives

We can also see functions as a little box: it has a name, an entry point, and an exit point. It is a little block that does something.

function box

When creating it, it encapsulates the task it executes and when using it, it is like a black box: I invoke the function, I don't care how it does it, I only know that it does it and it's useful for me to take the result.

black box

The function has a name: it has to be something that represents it, that indicates what it does, to be able to understand it.

representative name

The input are the parameters: parameters are the data, the variables that we pass to a function.

input parameters

Think of the function as a closed box: it can only use what we pass to it.

function isolation

And from the outside we don't see what's inside either, we only see the result.

exterior result

This leads us to the concept of scope: the ambit of variables, where they live, from where they can be used.

A function can create its own variables: they are local variables to the function, from the outside we don't see them.

local variables

And we also have global variables, which depending on the language are created outside or are declared as global variables, and can be used throughout the program, even within functions even if we don't pass them as parameters.

global variables

It is a bad practice to use global variables because they stay for the entire life of the program occupying memory, and the program becomes more difficult to debug, because it's hard to detect where the value of the variable is changed. So we forget about global variables and pass entry parameters to the function.

And the output, the result it returns, returns a single result.

single return

We must state what data type it returns.

return type

For example, we have the average function, which returns a decimal type and receives three parameters, three grades that can be of decimal type. It creates the local variable result, adds the three grades, divides them by 3 and returns that result.

average analysis

Where we want to use it, we invoke it with the name and passing the parameters. We can use it as many times as we want.

function reuse

# Advantages of using functions

  • Avoids duplicating code: we can invoke the function as many times as we need, we don't have to write the same code twice.
  • Use of libraries: we can use libraries of functions made by others, include them in our program and save time, favoring code reuse.
  • Teamwork: allows dividing work into parts, modules or functions. Each one does a part and then they integrate.

# Parameter by value and reference

Something important about functions are the parameter passages by value and by reference.

When we pass a parameter, a piece of input data, to a function, we can change its value within the function, and have this new value reflected outside of it. This is known as passing by reference.

To indicate passage by reference, the parameter is passed with a special indication, which depends on the language (for example, an ampersand &). If modified inside, the change is reflected outside.

reference

In passing by value, if the variable changes within the function, it doesn't affect the value outside; upon leaving the function it maintains the one it had upon entering.

value

To conclude the functions topic, best practices say that each function should perform a single task and that its name should be representative.

That's as far as we go with the functions topic. This is one of the basic topics that a programmer must master, and the only way to achieve it is by practicing; it's not enough to just read theory. In the next article we will see Arrays, a variable that allows storing more than one value.

Resources used