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!
Remember, a function acts only when we invoke or call it. Consider the function sayHello
, invoked by sayHello()
. Here's an illustration:
Function invocation in JavaScript is akin to using a recipe — you 'invoke' the recipe to make the cookies!
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:
Voila! Both "Hello"
and "Goodbye"
are presented together by invoking sayHello
and sayGoodbye
within greet
.
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:
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!
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:
If no name is given to greetPerson
, it uses the default name "stranger"
.
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!
