Passing Values to Functions with Argumentsr

Tell us what’s happening:

Your code so far


// Example
function ourFunctionWithArgs(a, b) {
  console.log(a - b);
}
ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.
function functionWithArgs(){
  console.log(1 + 2);
  console.log(7 + 9);
}
functionWithArgs(3, 16); // Outputs 3

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments/

my code is the one above i also tried this
// Example

function ourFunctionWithArgs(a, b) {

console.log(a - b);

}

ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.

function functionWithArgs(){

console.log(1 + 2);

}function functionWithArgs(){

console.log(7 + 9);

}

functionWithArgs(3, 16); // Outputs 3

its did not help please tell whats am doing wrong

You written the function without any parameters. This function takes two arguments:

function example(a, b) {

This function takes no arguments:

function example() {

You’ve done it like the second one. Look at the example: it should be almost exactly the same, but you’re supposed to sum rather than subtract

1 Like

thanks lets me try that and get back to you

Nah thats not it .

// Example

function ourFunctionWithArgs(a, b) {

console.log(a - b);

}

ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.

function functionWithArgs(1, 2){

console.log(1 + 2);

console.log(7, 9);

}

functionWithArgs(3, 16); //Output 3[quote=“MTHO2110, post:1, topic:243958, full:true”]
Tell us what’s happening:

Your code so far


// Example
function ourFunctionWithArgs(a, b) {
  console.log(a - b);
}
ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.
function functionWithArgs(){
  console.log(1 + 2);
  console.log(7 + 9);
}
functionWithArgs(3, 16); // Outputs 3

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Link to the challenge:

[quote=“MTHO2110, post:1, topic:243958, full:true”]
Tell us what’s happening:

Your code so far


// Example
function ourFunctionWithArgs(a, b) {
  console.log(a - b);
}
ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.
function functionWithArgs(){
  console.log(1 + 2);
  console.log(7 + 9);
}
functionWithArgs(3, 16); // Outputs 3

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36.

Hi there,

to make a function usable in different contexts, they are defined with parameters. Example, function test (a,b) {does something with the parameters}. Parameters are just placeholders (empty boxes). Each time you call a function, you pass in arguments that fill those boxes (so there are no longer empty). So, for example, when you call the test(2,3), the box “a” becomes 2, the box “b” becomes 3. Next time you call the function you can pass different arguments, example test(5,6), then “a” becomes 5, “b” becomes 6. I hope that helps.

1 Like

Let’s see the example. Here with have a function that accept two parameters a and b, and do something with those parameters. Then we call the function with ourFunctionWithArgs(10, 5); saying that a=10 and b=5 (because we have put the numbers in the parenthesis) . If you see the function you have written, you can’t do this, try to apply this logic to your function

function ourFunctionWithArgs(a, b) {

console.log(a - b);

}

ourFunctionWithArgs(10, 5);

Thats the thing i did exactly as you did , but only one output runs correctly .

You are not… see comments in the following code

function functionWithArgs(){ // no arguments inside the parenthesis
  console.log(1 + 2); //hard written numbers, you need to use only variables inside here (those you passed as arguments), no numbers
  console.log(7 + 9);
}
functionWithArgs(3, 16); // so here you are calling the function without assigning those numbers to anything

To pass multiple tests you need to make your functions general (parameters need to be able to act as variables and store arguments that the FCC environment passes for each test, so you cannot put numbers there…).

function test(a,b) {return a+b} is a general function
function test() {return 1+2}is not general and won’t pass more than 1 test (it will pass 1 if lucky)
function test(1,2) {return 1+2}is not general and is invalid in syntax

1 Like

this is my code right now

// Example

function ourFunctionWithArgs(a, b) {

console.log(a - b);

}

ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.

function functionWithArgs(a, b){

console.log(1 + 2);

console.log(7 + 9);

}

functionWithArgs(3, 16); //Output 3,16

// Example
function ourFunctionWithArgs(a, b) {
  console.log(a - b);
}
ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.
function functionWithArgs(a, b){
  console.log(1 + 2);
  console.log(7 + 9);
}
functionWithArgs(3, 16); //Output 3,16

you are not using a and b inside the function, try changing the inside of your function to use only variables and not hard written numbers

1 Like

You’re not putting arguments into the function. Look at the example:

function ourFunctionWithArgs(a, b) {
  console.log(a - b);
}
ourFunctionWithArgs(10, 5);

Your code does not look like that

Edit: I hadn’t seen your other post, what @leahleen says

Like this function:

function add1(x) {
  return x + 1;
}

The point of it is that you give it a number and it adds 1 to it. So add1(1) is 2, add1(25) is 26, add1(267) is 268.

If I write the function like:

function add1(x) {
  return 1 + 1;
}

It doesn’t work:add1(1) is 2, but add1(25) is 2, add1(267) is 2, add1(10000) is 2. This exactly equivalent to what you’ve done, you’ve just written a function that returns the same value every time you use it

1 Like

the requirements is that there should be an output of 13,16 which are hard numbers

below is the requirements :

functionWithArgs should be a function
functionWithArgs(1,2) should output 3
Passed
functionWithArgs(7,9) should output 16
Passed
Call functionWithArgs with two numbers after you define it.

okae thank you so much guys , i did it ,thanks a lot

1 Like

You’re misunderstanding the point of functions. The tests are there to check your code works, the values can be anything: there could be a million tests all with different values and your code should pass them all.

you were right the whole time , ive done it thank you

Even this steps helped me in learning. Thank you.

Selenium Training in BAngalore
Selenium Training in Marathahalli