What's the difference between the two?

I am currently doing the challenge: Passing Values to Functions with Arguments.

The exercise wants this:

  1. Create a function called functionWithArgs that accepts two arguments and outputs their sum to the dev console.
  2. Call the function with two numbers as arguments.
  3. functionWithArgs(1,2) should output 3
  4. functionWithArgs(7,9) should output 16

I got the answer correct with this:

function functionWithArgs(param1, param2)  {
console.log(param1 + param2);
}
functionWithArgs:functionWithArgs(1, 2);
functionWithArgs:functionWithArgs(7, 9);

but I wanna know what’s the difference between the one above and this:

function functionWithArgs(param1, param2)  {
return param1 + param2;
}

console.log(functionWithArgs(1,2));
console.log(functionWithArgs(7,9));

They both have the same output of : 3 and 16, but the one with return param1 + param2 gets an error of :

// running tests

functionWithArgs(1,2)

should output

3

. // tests completed // console output 3 16 3 16

Please enlighten me since I’m a beginner.

So, generally you want your functions to use return.

But, for this challenge, the instructions want you to make a function that

accepts two arguments and outputs their sum to the dev console

This means that you need a console.log() inside of your function.

The behaviour of the function needs to match exactly the requirements. Our tests check the behaviour of the function and not the behaviour of the function and other code you write alongside the function.

1 Like

Right, just to be clear, you have:

function functionWithArgs(param1, param2)  {
  return param1 + param2;
}

console.log(functionWithArgs(1,2));
console.log(functionWithArgs(7,9));

in your IDE, but the test isn’t basing what it checks on those bottom two lines. (You could delete them or change them.) The test is sending its own values to functionWithArgs and seeing if that function sends anything to the console.

2 Likes

@JeremyLT @kevinSmith

Thank you guys for trying to explain what happens and I’m sorry if I didn’t make myself clear as I am confused already. :confused:

I guess all I wanna know is when to use Return in general, not because it is required by a challenge or exercise? Because I’ve been reading a lot of tutorials and I always see them using return.

You will almost always use return. A console.log() just prints out the values, but a return statement lets other parts of your code use the returned value.

1 Like

Just to expand on what Jeremy is saying, it really depends on what you need. If you need a function that sums to numbers and logs it out, then the first version would be good, but I would call the function something like logSum. If all you want is the sum of two numbers, then I’d use the second call it getSum or something else descriptive.

Both are completely valid ideas. In the real world, I would expect more things like the latter example, but for this challenge, they wanted something that does what the former does.

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.