Functions and procedures

Página 28

Functions and procedures are fundamental elements in computer programming. Both are used to organize and structure code, making it more readable and maintainable. In addition, they allow the reuse of code snippets, avoiding unnecessary repetition of commands.

Functions

Functions are blocks of code that take one or more input values, process those values, and return a result. They are very useful when we need to perform a calculation or a specific operation several times throughout the program. Instead of repeating the same code in different parts of the program, we can create a function that performs the desired operation and call it whenever necessary.

For example, imagine that we need to calculate the total value of a purchase, considering the unit price of each item and the quantity purchased. We can create a function that takes the unit price and quantity as parameters and returns the total amount:


function calculateTotal(unitprice, quantity) {
  var total = unitprice * quantity;
  return total;
}

To use this function, just call it passing the desired values ​​as parameters:


var totalValue = calculateTotal(10, 3);
console.log(TotalValue); // prints 30

Procedures

Procedures are similar to functions, but do not return a value. They are used when we need to perform a sequence of commands without the need to return a result. For example, imagine that we need to display a welcome message to the user when starting the program. We can create a procedure that performs this task:


function displayMessage() {
  console.log("Welcome to the program!");
}

To use this procedure, just call it:


displayMessage(); // prints "Welcome to the program!"

In summary, functions and procedures are fundamental elements in computer programming. They allow code organization and reuse, making the program more readable and maintainable.

Ahora responde el ejercicio sobre el contenido:

_What is the difference between functions and procedures in computer programming?

¡Tienes razón! Felicitaciones, ahora pasa a la página siguiente.

¡Tú error! Inténtalo de nuevo.

Siguiente página del libro electrónico gratuito:

29Database

¡Obtén tu certificado para este curso gratis! descargando la aplicación Cursa y leyendo el libro electrónico allí. ¡Disponible en Google Play o App Store!

Disponible en Google Play Disponible en App Store

+ 6,5 millones
estudiantes

Certificado gratuito y
válido con código QR

48 mil ejercicios
gratis

Calificación de 4.8/5
en tiendas de aplicaciones

Cursos gratuitos de
vídeo, audio y texto.