Article image Functions and procedures

Functions and procedures

Page 28 | Listen in audio

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.

Now answer the exercise about the content:

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

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Database

Next page of the Free Ebook:

29Database

2 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text