Calling Functions

Calling Functions

🙋 Need help? Ask an expert now!

To call a function, we use the function's name, a set of parenthesis, as well a semicolon (optional but preferred). This will execute the statements inside the function. The function can be called from anywhere in the program, including a loop, another function, or even in itself (process called recursion).

The function printGreeting() is printed inside the function print() in the sample code above. This is why "Hello" is printed twice when the program is executed. The function fibonacci() is an example of recursion, a concept we will not be exploring in-depth in this course. Recursion is the act of calling the function in itself, as shown above. This creates a loop-like feature where the statements inside the functions are run multiple times before it returns a value. Feel fre to research more about recursion and how it works online. Knowing how to use recursion can drastically improve your programming skills and enable you to write more complex programs.

Edit Me on GitHub!

Application Question

Consider the following code segment:

function printGreeting() {
              console.log("Hello");
            }
            printGreeting();
            console.log("Hello");
            

How many times is "Hello" printed to the console?