Passing Values to Functions with Arguments

Hi There, I was stuck on this challenge and ended up having to copy/paste the solution from the video which worked however I am not sure if it is a bug or I am just not understanding the code properly but struggling to see where 3 and 16 come from.

function functionWithArgs(a, b) {
    console.log(a + b);
}

functionWithArgs(10, 5)

Hello! Welcome to the community :partying_face:!

Those values come from the parameters you pass to the function when you call it.

On this line functionWithArgs(10, 5) you’re calling the function and passing two parameters: 10 and 5

These parameters (10 and 5) are assigned to the variables a and b on the definition of the function here:

function functionWithArgs(a, b) {
    // At this point, a = 10 and b = 5
    console.log(a + b);
}

Does it help?

Check this out if my explanation doesn’t help: click here.


Also:

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Hi, thank you for replying however the tests on the challenge compared the output of the code do not match. I am unsure where 3 and 16 are being picked up from.

Ah! I see!

The tests call the function at lest two times:

  1. The function is called with the parameters 1 and 2 (a = 1, b =2), where 1 + 2 equals 3
  2. The function is called with the parameters 7 and 9 (a = 7, b = 9), where 7 + 9 equals 16

That explains the output of 3 and 16. The first 15, if you’re wondering, is from the parsing of the script, which includes the first call to your function as defined here:

function functionWithArgs(a, b) {
    console.log(a + b);
}

functionWithArgs(10, 5) // <-- First call

That’s why you see 15, 3 and 16 in the console.

right, so having gone through it out loud multiples, it is starting to make sense now. thank you for your help.

1 Like

No problem, we’re happy to help :slight_smile:!

Happy coding!

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