Return


#1

Hello, for what do i need RETURN ? i dont get it so much.
i can get the same results with the usual way.


var timesTwo = function(number) {
    return number * 2;
};

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



MY CODE:


var times = function (num) {
 var ex = num * 2;
 console.log (ex);
};

times (5)

both of the codes gives me the result - 10.


#2

With console.log you just print it out.
With return, you can use that what one func returns in another func.


#3

how can we print out the exact value of newnumber.?:unamused:


#4

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

// Call timesTwo here!
timesTwo(2);
var newNumber = timesTwo;
console.log(newNumber);


#5


#6

var timesTwo = function(number) {
    return number * 2;
};

// Call timesTwo here!
timesTwo(2);
var newNumber = timesTwo;
console.log(newNumber);

Well, what you do here is that you store the value that is stored in timesTwo in the variable newNumber. If you have a close look you'll see that this value is:

function(number) {
    return number * 2;
};

So as this is obviously a function the console.log correctly tells you: (This is a) [Function]. The 4 probably comes from a test of a subroutine checking if your code is correct.

What you want to do here is what @tagwhiz38863 already did meaning that you do not store timesTwo but the result of the function call of timesTwo. For example timesTwo is the function but timesTwo (5) means jump to the function named timesTwo and run it's code where you replace it's parameter with the value in the (), here 5. So it executes timesTwo with a value of 5 computes the result, which is 10 and then returns this value of 10. Now in the end this returned value of 10 is stored instead of the function.

and to expand on @puristaja's answer: Maybe think about what it would mean if programs/functions were humans. Then console.log's task would be to provide a human-to-paper interface. Meaning other persons tell this person something and this person just writes it down. Now:

var timesTwo = function(number) {
    return number * 2;
};

on the other hand would be a person who calculates something. So you tell him a number and he responds to you the double of that number. So when you use this:

var times = function (num) {
 var ex = num * 2;
 console.log (ex);
};

the person would still calculate the result but instead of telling you the result it will remain mute (towards you: undefined return value) but instead tells the writing person to put the result on a piece of paper. Now if you're main goal was to write down the result this is equivalent but as you should see the first version is far more versatile, because you could use the value for far more things than to just write it down.

For example you can make it part of another calculation:

timesTwo(timesTwo(2)+timesTwo(3))

Hope this helps.


#7

// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
return number * 2;
};
// Call timesTwo here!
timesTwo(8);
var newNumber = timesTwo;
console.log(newNumber);
its not working!!
I am stuck


#8

Hello @teraace17172 the instruction says:

  1. On line 7, after the equals sign, call the function timesTwo with any parameter you want.

example:

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

// Call myApples here!

var countApples = myApples(8);
console.log(countApples)

Hope this helps! :kissing_heart:


#10

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

// Call timesTwo here!
var newNumber = timesTwo(5);
console.log(newNumber);

You need to add the function of any parameter that is noted in bold


#11

hey lou03!
thanks...I figured that out:relaxed:


#12

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