Introduction and Overview

Welcome back! Today's mission takes us on a journey into TypeScript, where we'll focus on invoking functions, invoking stand-alone functions within another function, and using default argument values.

In JavaScript, a function execution is triggered when the function is invoked or called, and this holds true in TypeScript as well. Additionally, we can combine the outputs of independent functions by calling them within another function. If a specific value isn't passed, we utilize default argument values. So, are you excited to understand these concepts in the context of TypeScript? Let's get going!

Understanding Function Invocation

A function springs into action when we invoke or call it. For instance, consider a function named sayHello. Invoking this function would look as follows: sayHello(). Here is an example:

Function invocation in TypeScript is akin to giving out instructions — you invoke the instructions to produce the desired outcome!

Invoking Independent Functions Inside a Function

Often, we need to combine the effects of different functions. Invoking independent functions within another function proves handy in these situations.

Imagine having two separate functions, sayHello and sayGoodbye. If we were to create a new function called greet that calls both sayHello and sayGoodbye, it would look like this:

There you have it! By invoking sayHello and sayGoodbye within the greet function, both "Hello" and "Goodbye" display together.

Using Other Functions Results

While writing functions, it's common to use the result of one function in another. In these cases, you would call a function within another function.

Consider a function addNumbers that adds two numbers and another function calculateAverage that calculates the average of two numbers. To calculate the average, we first need the sum, so calling addNumbers within calculateAverage is necessary. Here's how we can accomplish this:

In our calculateAverage function, we call addNumbers which returns the sum of num1 and num2. We then use this sum to calculate the average.

Parameter Default Values

We've already covered this before but as a reminder, function parameters in TypeScript can have default values. If a parameter is not provided when calling the function, it uses the default value. Here's how it works:

If no name is passed to greetUser, it uses the default name, "curious coder".

Lesson Summary and Practice

Impressive! You've mastered invoking functions in TypeScript, learned how to invoke independent functions within a function, and tackled using default argument values for function parameters.

Next up some exciting exercises coming your way to strengthen your understanding and proficiency. Remember, practice reinforces concepts! As we continue our journey with TypeScript, stay engaged and excited!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal