0

#1

I am working on two assignments

1. Create a function called sum that returns the sum of two numbers passed in as parameters.
this is my code
var sum = function(2)(3) {
return (5)
};

2.Create a function called triple that takes in a number as a parameter, triples the number, and returns the result.

var triple = function(2) {
return (6)
};

#2

When you are defining a function you do not pass in the literal values. Instead you define it with variable names because the value you pass in might be different each time. You are passing in a number so instead of having a literal â€ś2â€ť replace that 2 with the a sensible name for a variable that stores a number. The word â€śnumberâ€ť or â€śnumâ€ť, â€śxâ€ť, â€śaâ€ť are all good candidates.

so for example if I wanted to quadruple a number using a function I would go:

``````var quad = function(num) {
return num * 4;
}
``````

You see that â€śnumâ€ť is the variable and how it is put in the function parameters: between the parentheses?

After the function has been defined you can now call it a bunch of different times:

``````var a = quad(1);
``````

You would have been shown something similar in the assignment: a function that is close to what you need that you have to change to make it do what you want. Taking functions and modifying them helps you learn how to write your own.

Your function that has 2 parameters is also incorrect in another way. You dont need to close your parentheses and open them again for each variable in your parameters, instead you separate them with a comma.

For instance:

function foo(a,b) {
//do something
}

#3

Create a function called sum that returns the sum of two numbers passed in as parameters.

var sum = function(2,3) {
return (5)
};
I ran the code like this but it still told me I am incorrect maybe Iâ€™m not fully grasping this.

#4

Hey berg,

Values are passed through to a function using a call in the main area of the program. For example, there will be a function call like below:

var sumResult = sum(2,3);

This will call the sum function, passing through 2 and 3 to the respective variables on the function. You will need to write the function to receive these variables, perform a task on them and return the result as below:

function sum(a,b){
//write the code to add a and b here
//write the code to return the sum of these added variables here
}

Some challenges that may help are below:

Hope that helped clear up your confusion!

#5

It did help but I am still a little confused this is what I wrote but now it is telling me Error >>>Expectedâ€™;â€™

Function sum = function(2,3) {
var 2+3
return (5)
};

#6

Hi berg25:

You need to first declare your function and pass in you parameters, i.e.
> function sum(paramter1, parameter2) {

``````// add code here
}
``````

Inside your function you usually want it to return the result of some code
So here you will return the result of the two parameters with the addition operator.

i.e.

function sum(number1, number2) {
return number1 + number2;
}

Donâ€™t confuse parameters with arguments, the parameter is just a place holder that is given its value when you pass in a value as arguments during the function invocation.

so in this case we will invoke the function add and pass in the two arguments 2 and 3

i.e.

the result of this function call will be 5

I hope this answers your question, please feel to reach out if you need any help and I can do a quick hangout with you to answer some of your questions.

#7

You misunderstand how functions work. You do not write a function that adds 2 and 3 and returns 5. You must write a function that adds `x` and `y` and returns the result. I only use `x` and `y` as an example, it does not matter how you call them in your function.

In this example I write a function that multiplies `x` and `y`:

``````function multiply(x, y) {
var product = x * y;
return product;
}
``````

`x` and `y` are parameters of multiply. Parameters are always variables like `x`, never literal values like `2`. What use would a function have if it could only add 2 and 3 and no other numbers? If we use placeholders instead of numbers, we can add any number to any other number.

Now I can use that function and give it actual numbers: `var test = multiply(2, 3);`
`test` is now `5`.

## How to write a function:

You try to define a function like this: `Function sum = function(2,3) {...};`, which is wrong.
There are two ways to define a function, either:

``````function sum(a, b) {
// some code
}
``````

or

``````var sum = function(a, b) {
// some more code
};
``````

Note that the second variants ends with a semicolon, while the first does not.

## Where to use the semicolon

I find this hard to explain, but I will try. I think there are two basic rules:

1. Donâ€™t end a `{ }` block with `;`
Example:
`function test() { // do something... }`

2. End everything else with `;`
Example:
`var myNumber = 42;`

A bit of a corner case is this already explained function definition:
`var sum = function(p, q) { // do something... };`
This is an anonymous function, because it does not have a name. It is assigned to the variable sum, but that is not the same as writing `function sum(p, q)`. Anonymous functions donâ€™t count as blocks when it comes to semicolons, so you should still add one at the end of the line.

#8

Personally Iâ€™m partial to the â€śdonâ€™t use semicolonsâ€ť philosophy, but since @QuincyLarson has said that the FCC code uses the airbnb style guide, Iâ€™ve gone back to using them (under protest)