# Helper Functions Exercise - Logic behind code solution

Could someone help me understand the logic behind the code below please?

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

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

const totalCost = costOfMonitors(5, 4);

console.log(totalCost);
´´´

From what I can understand the variable costOfMonitors(5, 4); assigns 5 and 4 to the parameters of rows and columns. I’m assuming it assigns these arguments to the parameters of the first function, monitorCount(rows, columns) and from there it then runs the code, multiplying 5 by 4 and then multiplying the result of this multiplication by 200.

What I don’t understand is why does the function costOfMonitors also has rows and columns as parameters and how does it affect the code, if it does affect it at all.

There’s probably a very clear and easy explanation but I can’t seem to find it.

Hey there and welcome to the forums!

So the first part of the code is the function definition of `monitorCount()`. This defines what actions the function is to take when called, and it gives it 2 parameters for use inside the function, `rows` and `columns`. This returns the product of rows and columns to wherever it was called.

The middle part of the code is the function definition of `costOfMonitors()`. This is a little different to the last one, as it actually calls the function `monitorCount()` inside of it. This means that whenever `costOfMonitors()` is called, `monitorCount()` is going to be called also. As `monitorCount()` requires 2 arguments rows and columns, as we mentioned in the previous paragraph, it cannot be called without being given values for these arguments.

Since `costOfMonitors()` wants to call `monitorCount()`, it needs 2 values to provide to that function in order to actually get the value needed for it (`costOfMonitors()`) to be returned correctly. As such we also give this function 2 parameters, `rows` and `columns`. This way when you pass the values into `costOfMonitors()` these make their way through to `monitorCount()` and the code can execute correctly. If you removed `rows` and `columns` in the definition for `costOfMonitors()` and also removed `5` and `5` from the function call when defining `totalCost` you will see this in action.

function monitorCount(rows, columns) { return rows * columns; } function costOfMonitors() { return monitorCount(rows, columns) * 200; } const totalCost = costOfMonitors(); console.log(totalCost);

Note how in the code block above, we have removed these, and the code now throws an error. It’s looking for a variable or argument named `rows` for `monitorCount()` but it cannot find any. Therefore the code returns an error.

Apologies for the long message, but I wanted to try give you as much info as possible so it can hopefully be clarified! Happy to clear anything up if necessary.

Thank you, so a function to be called always has to have parameters correct?

Unless the parameters have default arguments then yes, you always need to call a function with arguments where it has parameters when defined.