Hi @adeniyi.oye,
The challenge is requiring that you complete two parts of the solution
- Create a function called functionWithArgs that accepts two arguments and outputs their sum to the dev console.
- Call the function with two numbers as arguments.
You already have the function defined, which is the first part of #1 above. You need to use console.log
to ouptut the sum of the two arguments passed in. And don’t forget to call your function at the end of your script, passing in two values.
I guess I should back up and ask if you are comfortable with functions and how they worK?
The general concept is this. If you have some code that you want to package up and reuse it in multiple places within a program, you can basically take that code and put it into a function.
So, for example lets say you have some formula—and I’m using something simple here just for making the example as simple as possible, but you can imagine this as being something more complex with more lines of code—that is implemented with this code:
var x = 4;
var y = 5;
console.log(x * y);
The code above is fine, but you may have to reuse this formula in many different places in your code. So without functions you’d have to write this every time you needed it , adjusting the x
and y
values depending on what is needed. Whenever you duplicate code and are rewriting it, or even copying and pasting it, there is a chance that you will introduce an error when duplicating it. This is where the power and flexibility of functions come along.
I can take the code above and package it as a function like so:
function multiply(x, y) {
console.log(x * y);
}
To package the code up as a function, I used the keyword function
followed by the name I want to give the function, in this case I used multiply
but I could have used anything to name the function as long as it is a valid name according to JavaScript’s syntax rules. Then I took the variables I was using previously (x
and y
) and I just made them parameters by placing them inside of parenthesis after the function name. It can be stated that this function named multiply
accepts two parameters, x
and y
. After that I have a code block that starts with an open curly brace {
and ends with a closing curly brace }
. Any code inside of the curly braces will run when I call the function. Which brings us to the question, what is “calling a function?”
When we have a function definition in our code like the one above, the code inside the curly braces doesn’t actually run when the JavaScript engine processes the lines of code that make up the function definition. Instead JavaScript just knows: hey, I have a function named multiply
so if I ever see a call to it I’m going to run this code.
To actually call the function, you use the function name and “pass in” a value (or argument as they are called) per each parameter in the function definition (in general). That looks something like this:
multiply(4, 5); // Logs 20
You can see that calling a function requires you to use the function name followed by a value for each parameter inside of parenthesis. In the line above I’m passing the Number value 4
as an argument to parameter x
and the Number value 5
as the argument to parameter y
. When this line of code runs, conceptually, the JavaScript engine recognizes this as a function call, will look up the function and know that it should assign the two argument values to the function parameters, and then run the code block belonging to the function definition.
This makes the code more versatile because now I can vary the values I pass into the function to multiply different values:
multiply(10, 20); // logs 200
multiply(6, 6); // Logs 36
//... etc.
I hope that clarifies things and helps you to solve this challenge…