Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 4 - Functions

4.3 Returning Values

Functions can also return values using the return keyword. This allows the function to send data back to the code that called it.

function add(a, b) {
    return a + b;
}
console.log(add(3, 2));

In the above example, the function add() adds a and b mathematically and returns the resulting value, which we print through console.log().

Let's create a function that solves the equation x + y - z.

function solveEqu(x, y, z) {
    return add(x, y) - z;
}

// Function to add two numbers
function add(x, y) {
    return x + y;
}

console.log(solveEqu(3, 5, 2));

Notice that the function solveEqu() depends on the function add(). This approach is known as the nested function where multiple functions are interconnected with each other.

Try to edit the code and apply a different equation.