Mastering TypeScript Functions: Invocation, Composition, and Defaults

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:

TypeScript
// Function is defined (like a blueprint for now)
function sayHello() {
  console.log("Hello!");
}

// Function is invoked (blueprint turned into reality)
sayHello();  // Output: Hello!

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:

TypeScript
// Defining independent functions
function sayHello() {
    console.log("Hello!");
}

function sayGoodbye() {
    console.log("Goodbye!");
}

// Invoking them inside another function
function greet() {
    sayHello();
    sayGoodbye();
}

// Calling the greet function
greet();
/*
Prints:
Hello!
Goodbye!
*/

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:

TypeScript
// Define `addNumbers` function that returns the sum of two numbers
function addNumbers(num1: number, num2: number) {
  let sum = num1 + num2;
  return sum;
}

// Define `calculateAverage` function
function calculateAverage(num1: number, num2: number) {
  // Call addNumbers inside calculateAverage
  let sum = addNumbers(num1, num2);
  let avg = sum / 2;
  return avg;
}

// Test the function
let average = calculateAverage(10, 20); // Output: 15

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

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