Function


#1

Nice job! Now, when we call a function, we don't always want to just print stuff. Sometimes, we just want it to return a value. We can then use that value (ie. the output from the function) in other code. Let's learn about the return keyword, then we'll see how to use functions with an if / else statement in the next exercise!

The return keyword simply gives the programmer back the value that comes out of the function. So the function runs, and when the return keyword is used, the function will immediately stop running and return the value.

Instructions

In our example we have a function called timesTwo() that takes in a number and returns the number multiplied by two.
01. On line 7, after the equals sign, call the function timesTwo with any parameter you want
02. Line 8 prints out newNumber. Notice how the value we return from timesTwo() is automatically assigned into newNumber.

?

Hint

To call the function, we just use the name of the function. We then put in a value for the number parameter. eg. timesTwo(8);

This is what i wrote.

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};

// Call timesTwo here!
var timesTwo = function (timesTwo)
{return (timesTwo * 2)};

timesTwo (5);

But its on about a function, no idea.


#2

Try this:

var newNumber = timesTwo(5);
console.log(newNumber);

It's like "var timesTwo = function(number)" but instead of "function(number)" you have to put the function "timesTwo" with a parameter your choice. Instead of "var timesTwo", you have to write "newNumber"

Sorry for my bad english but I hope that you understand what I mean :slight_smile:


#3

Thanks it worked, your English is perfect, I couldn't even understand it myself, are you a programmer or another learner like me?


#4

Cool, I'm glad that it works!
I'm a newbie, I've started learning about month ago :slight_smile:

Good luck with another lessons! :smiley:


#5

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