9. functions with two parameters ~Help needed!~


#1



9. functions with two parameters


The error is "SyntaxError: expected ';'


// Write your function starting on line 3
{
var perimeterBox(1, 7) = function(length, width);
return length + length + width + width;
};


#2

You're function syntax is wrong.

The structure of a function should look something along the lines of this:

var functionName = function (parameter1, parameter2) {
    return parameter1 + parameter2;
};

To call the function simply write the function name with the values between the parameters:

functionName(15, 32);

#3

Thanks for the help, but I'm still having some errors. Here is my new code:

// Write your function starting on line 3

var perimeterBox(15, 32); = function (length, width); {
return length + length + width + width;
};

The error is SyntaxError: expected ';'


#4

//      function name
//           |
//           v
var perimeterBox(15, 32); = function (length, width); {
return length + length + width + width;
};

Your function name shouldn't have parenthesis and filled in parameters, this comes later when you call the function.
And semicolons are only needed after a full statement (not really sure how to explain this in a good way) so for example:

//                no semicolons needed           these are necessary
//                |                 |            |  |
//                v                v             v  v         
var sayHello = function() { console.log("Hello!"); };

#5

Let's say we create the function sum and it takes two values (parameters). What we want this function to do is to return the two values added to each other. For example: sum(2,3) should return 5 because 2 + 3 = 5.
The code would look like this:

//   1        2       3      3     4
var sum = function(value1, value2) {
//    5       6   6    6  7
    return value1 + value2;
//8 9
};

//            10
var number = sum(2, 3);

Let's walk through this code, step by step:

1) We define a variable named sum

2) We let javascript know that what we store in the variable will be a function

3) Wel tell javascript that this function will take two values (parameters), respectively named value1 and value2

4) The contents of a function are enclosed by curly brackets ( { and } ). Here we let javascipt know that what's coming next are the contents of this function, the code that should be executed when the function is called.

5) We don't do much in this function so we immidiately go to the return statement. This keyword shows javascript what we want to return.

6) Here's our math, what we created our function for. Here we take the first value, value1, and add the second value, value2, to it. Because this comes on the same line (before the semicolon) as the return keyword the result of this small calculation will be returned from the function.

7) Because we're done with this line of code, we tell javascript nicely that this is the en of the line with a semicolon.

8 & 9) That's all our function does, so we tell javascript that that was all the code that should be executed when the function is called. We do this by ending with a closing bracket. And because this was the end of declaring the function in a whole, we tell javascript we are done declaring, this is again done with a semicolon.
(For as far as I know this semicolon at step 9, isn't mandatory)

10) And finally we call our function by filling putting two values between the parenthesis to add to each other. Here we store it in a variable called number. number now contains the output of our function, in this case, the number 5 because 2 + 3 = 5.


I hope this step-by-step explain helped you further.
(I also hope it wasn't to basic)


#6

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