Lesson 2

Journey Through JavaScript: Function Invocation, Combining Functions, and Default Arguments

Introduction and Overview

Welcome back, Explorer! Today's adventure dives into JavaScript, exploring function invocation, calling independent functions within a function, and working with default argument values.

A function invocation refers to calling or triggering a function. Invoking independent functions within another combines their outputs. If no specific value is passed, default argument values are used. Are you ready to decode these concepts? Let's get started!

Understanding Function Invocation

Remember, a function acts only when we invoke or call it. Consider the function sayHello, invoked by sayHello(). Here's an illustration:

1// Function defined (just a blueprint for now) 2function sayHello() { 3 console.log("Hello!"); 4} 5 6// Function invoked (blueprint turned into reality) 7sayHello(); // Output: "Hello!"

Function invocation in JavaScript is akin to using a recipe — you 'invoke' the recipe to make the cookies!

Invoking Independent Functions Inside a Function

Very often, we need to combine the outputs of different functions. That's when invoking independent functions within another becomes handy.

Imagine you have two distinct functions, sayHello and sayGoodbye. Now, crafting a greet function to invoke sayHello and sayGoodbye would look like this:

1// Defining independent functions 2function sayHello() { 3 console.log("Hello!"); 4} 5 6function sayGoodbye() { 7 console.log("Goodbye!"); 8} 9 10// Invoking them inside another function 11function greet() { 12 sayHello(); 13 sayGoodbye(); 14} 15 16// Calling the greet function 17greet(); 18/* 19Prints: 20Hello! 21Goodbye! 22*/

Voila! Both "Hello" and "Goodbye" are presented together by invoking sayHello and sayGoodbye within greet.

Using Other Functions Results

Often, while writing functions, you would want to utilize the result of one function inside another. In such cases, you would call a function inside another function.

Take an example of a function findSum that adds two numbers and another function getAverage that finds the average of two numbers. To get the average, we must first find the sum. Hence, we need to call findSum inside getAverage. Here's how it is done:

1// Define `findSum` function that returns the sum of two numbers 2function findSum(num1, num2) { 3 let sum = num1 + num2; 4 return sum; 5} 6 7// Define `getAverage` function 8function getAverage(num1, num2) { 9 // Call findSum inside getAverage 10 let sum = findSum(num1, num2); 11 let avg = sum / 2; 12 return avg; 13} 14 15// Test the function 16let average = getAverage(10, 20); // Output: 15

In our getAverage function, we first call the findSum function, which returns the sum of num1 and num2. This sum is then used to calculate the average. This is how you can call functions inside functions!

Parameter Default Values

In JavaScript, function parameters can have default values, which allow the parameter to be optionally omitted when calling the function, using this default value for it instead. Here is how it works:

1// Specify 'stranger' as the default name 2function greetPerson(name = "stranger") { 3 console.log(`Hello, ${name}!`); // String interpolation to include the name in the greeting 4} 5 6// Call greetPerson without providing a name 7greetPerson(); // Output: "Hello, stranger!" 8 9// Call greetPerson with the name "Bob" 10greetPerson("Bob"); // Output: "Hello, Bob!"

If no name is given to greetPerson, it uses the default name "stranger".

Lesson Summary and Practice

Bravo! You've mastered function invocation in JavaScript, implemented the concept of invoking independent functions within a function, and defined default argument values for function parameters.

Get ready for a series of fun exercises to reinforce your understanding and proficiency. Remember, practice makes perfect! As our JavaScript journey continues, stay on board and stay tuned!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.