Function return

Can someone please explain how I get to 5?

function test(number) {
while(number < 5)
{
number++;
}
return number;}

alert(test(2));

→ 5

When number is 4 your “while” is doing the command “number++” (so your number is 5 now) and stops. Function “test” is returning the last value of number which was 5.

1 Like

Thank you for a great reply :slight_smile:

I tried intially Stack Overflow and was told to Google it … I’ve yet to be impressed with SO! :no_mouth:

function test(number) {

while(number < 5) {  // number less than 5 = 4

number++;  // 4 ++ (increment) = 5

} return number; }  // return a value from the function

alert(test(2)); 

→ 5

OK … that now makes sense. But where does the 2 come into it? or is it because the function stops at the return?

First of all please don’t use = to refer to equal. That is actually assignment symbol and it makes your comments confusing. Use == or === instead.

If you got:

[quote]

function test(number) {

while(number < 5) { // number less than 5 = 4

number++; // 4 ++ (increment) = 5

} return number; } // return a value from the function

alert(test(2));[/quote]

Then this is exactly what happens:

function body gets ignored. It’s a set of instructions that are only followed when specially invoked through a function call.

The first thing that gets evaluated is alert(test(2)); This is the function call. Without the function call the function body itself would slumber in limbo for all eternity.

If the code would be compiled and executed line by line then the function declaration line would be useless. This isn’t a special paragraph or citation. Look at it as if you were reading a book and there was a black and white picture next to the text … you get to a certain paragrah and only then the picture becomes relevant, this would be similar to the function call [ look now at the picture to better understand the scenery ]

So what happens is the following:
alert(test(2)); > invokes function [ the list of instructions with the piece of information 2 ]

function test(2) {

while(2 < 5) {

2++;

}

while(3 < 5) {

3++;

}

while(4 < 5) {

4++;

}

while(5 < 5) { }

return number; }

Note that curly bracket symbol is the function end marker. The function body goes from { to }
I guess older programming languages like pascal made things easy because there was a begin and end declaration.

2 Likes

Eureka … thank you ever so much :grin:

Great explanation. That’s also solved, a few other similiar querys I had.

Thanks to all :tada:

Actually function is doing the “return” when “while” loop stops. The whole function says “Hey! Give me a number (for example 2) and I will increment it untill it’s smaller than 5. Then I will increment it one more time and return it to you.” So there’s no sense you gave it a 2 or 1, it will return 5. :slight_smile:

1 Like

I think the problem he is having is that he has too much to take in all at once.

oh btw nigel, the function will return a value whenever it reaches the first return and then stops executing, for example:

function () {
return 0;

[huge amount of lines ]
}

will simply return 0 and then the rest of the code in the function is completely ignored

1 Like

Thank you :slight_smile: