Chapter 14: Functions and Procedures
In programming, functions and procedures are blocks of code that perform specific tasks and can be reused in different parts of the program. This makes the code more organized, easier to understand and maintain. We will explore these two concepts in detail in this chapter.
Functions
A function is an independent block of code that performs a specific task. It can be called at any time during program execution, from anywhere in the code. Functions usually take parameters and return a value, but this is not strictly necessary.
To define a function, we use the 'function' keyword followed by the function name, parentheses, and a block of code enclosed in braces. Parameters are specified between parentheses. For example:
function sum(a, b) { return a + b; }
This is a simple function that adds two numbers. To call this function, we use the function name followed by parentheses and the arguments we want to pass. For example:
var result = sum(5, 7);
The 'result' variable now contains the value 12, which is the result of adding 5 and 7.
Procedures
A procedure is very similar to a function, but with one fundamental difference: a procedure does not return a value. Instead, it performs a task and then finishes. Procedures are useful for tasks that need to be performed multiple times but don't need to return a value. For example, we might have a procedure that prints a message to the screen.
To define a procedure, we use the keyword 'procedure' followed by the procedure name, parentheses, and a block of code enclosed in braces. For example:
procedure printMessage() { console.log("Hello world!"); }
To call this procedure, we use the name of the procedure followed by parentheses. For example:
printMessage();
This will print the message "Hello world!" on the screen.
Importance of Functions and Procedures
Functions and procedures are fundamental in programming because they allow code to be reused. Without them, we would have to write the same code over and over again to accomplish the same task. In addition, they make the code more organized and easier to understand, since each function or procedure performs a specific task.
In addition, functions and procedures also make code easier to maintain. If we need to change the way a task is performed, we only need to change the code in one place: in the function or procedure that performs the task. This is much more efficient than having to look and change the code in several different places.
In short, functions and procedures are powerful tools that make programming more efficient, more organized, and easier to understand and maintain. They are a fundamental concept that all programmers must understand and use.
Exercises
Let's practice what we've learned in this chapter. Try writing your own functions and procedures to perform simple tasks like adding two numbers or printing a message to the screen. Then try writing more complex functions and procedures that perform more complex tasks. Remember to test your code to ensure it works correctly.