Why do I need to use `return`?

Hi i see where your confusion lies. What they are saying is that when using the return function in the same block of code when one return function executes the code block end there and all code after will not be executed
check this code of mine out
function usingTheReturn(numOne, numTwo) {

if (numOne < 5 || numTwo < 5) {

    return 'I will return this if numbers are less than 5 and nonthen else';

} else {

    return numOne + numTwo;

}

}

const mathProblem = usingTheReturn(4, 10);

console.log(mathProblem);
//This will log I will return this if numbers are less than 5 and nonthen else
// change the 4 to 6 and 16 will be printed

This is where we run into a problem. return is not a function, but a property of a function. If said function exists, then its return value exists also, as a property of the function object. Make sense? Letā€™s explore this further on your go aheadā€¦

2 Likes

This is where it gets really messed up. The return of any function is not a value, but a fixed point in memory. It is the point to which it is to return from the function. The value aspect is an entirely different realm.

1 Like

Thank you for clearing up that context for me still learning terms and definitions perfectly explained at least for me

1 Like

I couldnā€™t quickly find an answer to the question of "Why do I need to use ā€˜returnā€™? Here is specifically what Iā€™m wondering: It seems to me that I could either use the return keyword or I could simply write my function and call it and get the same result. Like such:

Option 1:

function monitorCount(rows, columns){
return rows * columns;
};

const numOfMonitors = monitorCount(5, 4);

console.log(numOfMonitors);

The above code provides an answer of 20.

Option 2:

function monitorCount(rows, columns){
console.log(rows*columns)
};

monitorCount(5,4);

This code also provides the answer of 20. So why do we need to use the return keyword? Iā€™m sure Iā€™m missing something so any help would be appreciated.

1 Like

It depends on whether you want your program to remember the value (assign a variable to it), or just print it to the console. console.log() prints its arguments to the console. return returns. They are not related or interchangeable.

2 Likes

I have to admit this exercise was VERY confusing, no because of the order of the logic, but because of the purpose of the ā€˜returnā€™ keyword. Considering that you can easily create a function the calculates the area of a square and prints it to the screen via console.log within the function and than call on the function without using ā€˜returnā€™ makes the keyword seem useless for example:

function monitorCount(row,column) { console.log(row*column); } monitorCount(9,4)

This works fine for calculating and printing something on the fly and real quick, without having to use ā€˜returnā€™. However when you toss variables into the mix such as ā€˜letā€™ and ā€˜constā€™ then things get more complex and and 'return must be used in order to calculate/run the function properly. Without ā€˜returnā€™ functions with data saved in variables will not. . . . Well function. It does not seem much right now but I imagine that if you have a function thats 50+ lines long you will want to use ā€˜returnā€™ for sure compared to console.log or am I wrong?

Anything that long is not a function, but a program. Functions are code snippets. Short, concise, consistent, reusable blocks of code that accept arguments, and return predictable results.

Sure we can log the outcome of a function and call it a done deal. Thatā€™s not a problem, though it is rather limiting of the potential of our functions to be useful after the fact. No returns means no useful data in the interim or later. Return the data (and log if so be it, either in the function or at the caller) and the data is captured and reusable.

Bottom line, write functions to perform an analysis and/or operation and return expected results for further use by the program. Logging is something for the developer, not the user. Keep the data moving along the chain.

1 Like

Why do we still need to declare another variable (const numOfMonitors) when we can only use one?

function monitorCount (rows, columns){
return monitorCounts = rows * columns;
}
console.log(monitorCount(5,4));

This also gives the same output.

1 Like

I did not see any recent answers to simplify this confusionā€¦ GLOBAL SCOPE VS LOCAL SCOPE the reason you cant console.log(numOfMonitors) is due to it exists only inside of the function. If you created the variable outside of the function it will then be able to console.log. hope this was helpful!

@mtf explains this as follows ā€¦

In other words, if we directly log the returned value (from the function) to the console using ā€¦

function monitorCount(rows, columns) {
   return rows * columns;
}

console.log(monitorCount(5,4));

ā€¦ then the returned value (20) is not stored anywhere for further use. This is fine if all we need to do is log it to the console, but in a fully functional program we are more likely going to need to manipulate this value further and possibly display it dynamically to the user as part of the HTML content rendered on their user interface.

To be able to access and reference the returned value later in the programā€™s control flow, after it has been returned from the function, we need to assign and store it in a variable outside the functionā€™s local scope. By assigning the function call itself to a variable, whenever the function call is executed, its output (the return value) will be assigned to this variable. If we need to, we can then log its value to the console by referencing the variable name within console.log(), and still retain it within the variable for the other uses Iā€™ve mentioned ā€¦

function monitorCount(rows, columns) {
   return rows * columns;
}
const numOfMonitors = monitorCount(5, 4);
console.log(numOfMonitors);
1 Like

So weā€™re sure we understand variables and values: Number values are stored as floats; JS doesnā€™t have an integer type, only methods to arrive at one. It is kind enough to present our integers as integers, but that is not how they are stored. Variables are not values, per se, but references to memory locations where the values are physically stored.

Apart from that we need a mechanism that allows sharing of a variable in one scope with another scope. That mechanism is the function argument and the function return. In both cases a value may be seen from one scope that exists in another.

Return values donā€™t really move from one point to another, they stay where they exist in memory, even though the function has ended. That is because what was actually returned, and correct me if Iā€™m wrong, is the reference to the value so that a new binding is created, allowing the value to remain in memory during garbage collection.

The same can be said of arguments, which also do not move from their spot in memory. Only the reference is given. However, since the parameter is locally declared if it changes inside the function the parameter variable takes the new value and argument value is unchanged.

This is confusing, and gets a little more confusing when the argument is a data structure (array or object). In that instance any mutation within the function has its side effect in calling scope, to the argument object itself. Scope has never changed. No point returning anything unless it is an aside.

Bottom line, data doesnā€™t move about, only its location is given so a binding can be established.

I understood it as

  1. create the function
    2.create the constant variable and use the function to get the number of monitors
  2. log the constant variable

It can work without any Variables.

function monitorCount(rows,columns){
  return rows*columns;
}
console.log(monitorCount(5,4));
// Prints: 20

A variable assigned inside the function body cannot be used outside the function. Therefore, in the task we assign it outside the function, so that it would be possible to use it in the future.

2 Likes

You definitely made sense.

this is how i done it.

let numOfMonitors = monitorCount(5,4) ;

function monitorCount(rows, columns) {

return numberOfMonitors = rows * columns ;

}

console.log(numOfMonitors);

its ticked everything but i feel like ive failed to get the concept or am i good here??

It was a bit confusing but if you follow the steps one at a time your code should come out correct.
for instance:

Step 1: asks you to declare a function monitorCount() that has two parameters. The first parameter is rows and the second parameter is columns .

function monitorCount(rows, columns) {
}

Step 2: In the function body of the function you just wrote, use the return keyword to return rows * columns

function monitorCount(rows, columns) {
return monitorCount = rows * columns;
}

Step 3: Declare a variable named numOfMonitors using the const keyword and assign numOfMonitors the value of invoking monitorCount() with the arguments 5 and 4

function monitorCount(rows, columns) {
return monitorCount = rows * columns;
}
const numOfMonitors = monitorCount(5, 4);

Step 4: To check that the function worked properly, log numOfMonitors to the console

function monitorCount(rows, columns) {
return monitorCount = rows * columns;
}

const numOfMonitors = monitorCount(5, 4);
console.log(numOfMonitors);

Output: 20

For me, best way to learn is to understand the concept through these lessons, go ahead and find other ways to approach which way fits your coding styles.
I also have an advantage of having a friend who already knows JavaScript to help and explain what I donā€™t understand.

Hope that helps you. It helped me to just follow then adjust to how I want to code.

I am really confusedā€¦why is there a need for a constant to be thrown into the mix when we can get the same result using the same variable defined in the function above? The following would result in the same answer and is more concise and to the point in my understanding. The commented lines are the actual solution to the exercise while the console.log line is the alternative:

function monitorCount(rows, columns) { return rows * columns; } //const numOfMonitors = monitorCount(5, 4); //console.log(numOfMonitors); console.log(monitorCount(5, 4));

Just seems like unnecessary overcomplication of code.