Logo of Computer Classes

What are the functions in basic programming

A function is a set of instructions that are grouped together to perform a specific task and can be easily reused.

There are times that within a program several instructions are repeated many times, especially in very long programs.

In order not to have to repeat them over and over again, we group them within a function and each time we want to activate them we will only have to "call" or "invoke" the function in the part of the program that we need it.

In short, a function is a piece of code that does something specific and that we can use in any part of our program and as many times as we want.

Let's see how a function is created in almost all programs directly (without pseudocode).

function function_name () {function instructions} The function name is used to call or invoke that function when we want it to be executed.

The concept is the same as with variables, which are assigned a unique name so that they can be used within code.

After the name of the function, two parentheses are included, the meaning of which is detailed later.

Finally, the {} symbols are used to enclose all statements that belong to the function (similar to how statements are enclosed in if structures).

Let's see an example:

number1 = 5;

number2 = 9;

function sum () {result = number1 + number2; Write "The result is" + result; }

To "call" or "invoke" the function within the program we will only have to write its name as follows:


Notice that we have declared the variables that the function will use before the function.

We could also put them after the function, but always before calling the function.

Every time the program reads add (); will perform the function.

Normally the functions that we are going to use in the program are defined at the beginning of the program, for that we must be very clear about what functions we will need.

Most of the time if variables are only used and exist inside the function they are put inside the leading parentheses (which we haven't used so far).

This is called putting "arguments" to the function. The variables (can be constants) that functions need to work are called "function arguments".

NOTE these variables only exist within the function and we cannot use them in another part of the program, that is why they are called local variables. Look at the example:

function add (x, y) {

var total = x + y;

alert (total);


add (1,2);

add (5.7);

First do the addition giving values ​​x = 1 and y = 2 and then do another sum with different values ​​for x and y (5 and 7).

In this case the variables are not declared before the function (or anywhere), they are variables that only the function contains.

To change the value of the variables is done by calling the function and setting its values.

NOTE the variables x, y and total cannot be used in another part of the program, since they are local suns.

With this basic knowledge, you are ready to learn to program in any high-level programming language.

Now you choose the one you want to learn, you will see that with this knowledge it will be very simple, just learn a little vocabulary of the programming language in English.


Before finishing we leave you a link with several exercises in pseudocode and their solution so that you can try to do them and practice.

You already know going from pseudocode to programming language is very easy, that is why it is very important that you master pseudocode.

Date update on 2021-03-31. Date published on 2021-03-31. Category: Computer class Author: Oscar olg Fuente: areatecnologia