# eHow?

## Recommended Posts

How does "return" works in a function()?e.g:

`function name(a,b){return a+b;}`

Edited by O. Samuel

##### Share on other sites

yeah! I have seen that before, but i need more xplaination!
##### Share on other sites

yeah! I have seen that before, but i need more xplaination!
you'll have to wait for one of the mods then.
The return StatementThe return statement is used to specify the value that is returned from the function.So, functions that are going to return a value must use the return statement.The example below returns the product of two numbers (a and b ):
did you try the "Try it yourself" button ? it really should be clear if you fiddle about in the Editor window and see how it changes. Edited by edGetItTypee
##### Share on other sites

A function behaves like a variable. It can be printed, or assigned to another variable. The value of the function when it's used would be what is returned from the function.Here's a quick example:

`function five() {  return 5;}alert( five() ); // The browser will display "5" in a dialog box`

.

##### Share on other sites

yeah! I have seen that before, but i need more xplaination!
in the example you post, it would return the sum of a+b.
`function name(a,b){  return a+b;  //returns the sum of the two params, a and b};var x = name(2,2);alert(x);  //alerts 4 (the sum of 2 + 2).var y = name(12,20);alert(y);  //alerts 32 (the sum of 12 + 20).`

the value of a+b is returned to whatever makes a call to the function, in these cases, since we are using this syntax

`var x = name (2,2);`

the return value of the function will be assigned to a variable, which can be used later. or instantly in Ingolme's examples.

`alert(name(2,2));`

Edited by thescientist
##### Share on other sites

Thanks guys, it kinda clear to me now!

##### Share on other sites

What about return true and return false? Y is it that there is "Return" before the submit button call the function()?

`...onclick="return function()..."`

Edited by O. Samuel
##### Share on other sites

In the case of event attributes. The attribute is like a function:

`function onclick() {  return something();}`

This returns the value that was returned by the something() function.When false is returned from an event, the default action of the event is cancelled. For example, if you return false on a link, the link won't open the page it's supposed to.

##### Share on other sites

in that case it's returning the return value of the function to the onclick event handler, at which point a true or false will either allow the default behavior of the onclick to execute or not. it is commonly used when submitting forms and attaching a form validation method to the submit button. If validation is successul, the user defined function will return true and the form will submit. if validation fails and the function returns false, the default submit action will not occur, the form will not submit, and the user will stay on the page.

Edited by thescientist
##### Share on other sites

isn' t the boolean value such a heaven-sent ?!

##### Share on other sites

Programming can't exist without booleans. Boolean algebra is the base of computers.

##### Share on other sites

...Boolean algebra is the base of computers.
and possibly "Dubbya" ? was just commenting that because i don't remember it existing in BASIC, not that i remember too much of it from way back when.with variables being checked against actual values etc, i certainly didn't know of such possible constructs(?) as ;if(boolean variable) then (statement).the if(condition) was always "(variable [=,<,>] condition)"maybe JSG would know...
##### Share on other sites

and possibly "Dubbya" ? was just commenting that because i don't remember it existing in BASIC, not that i remember too much of it from way back when.with variables being checked against actual values etc, i certainly didn't know of such possible constructs(?) as ;if(boolean variable) then (statement).the if(condition) was always "(variable [=,<,>] condition)"maybe JSG would know...
I'm not sure what you are trying to say, so maybe you already get this, but control structures base their 'flow' around the truthiness of a statement. All conditions, be it <, >, ==, ===, etc resolve down to either a true or false condition.
`var x = 2;if(x == 2){  console.log('x is equal to 2');}else{  console.log('no it's not');};`

basically, if x == 2, then that statement is true. the code within the if block is executed. it can also be done just as simply as this

`var x = 234324;if(x){  console.log('x has been initialized, hooray!');}else{  console.log('woops, no it's not...');};`

see what happens if you assign null to the value of x. I think what Ingolme is getting at is machine/assembly language, where in all programming languages eventually get crunched down to series of 0's and 1's. (on/off, true/false). It is the fundamental principle of electronics and programming. All a computer understands is byte code.

##### Share on other sites

Well, even further down than assembly level it's all based on boolean algebra.A sum is simply using the boolean OR operation between two bits.A sum of numbers composed of several bits is a combination of OR and AND operations to work with carry bits.A subtraction is the sum of a number with the compliment of the second number.A multiplication is several sums, a division is several subtractions.

##### Share on other sites

I'm not sure what you are trying to say, ...
i meant when boolean variables were first introduced, hence the reference to JSG, hinting at his "know your history" sig.that is, conditions were always comparisons of a variable to an explicit value, the variable did not have an inherent boolean value; at least not in BASIC, or what basics(!) i learned of it.
##### Share on other sites

Boolean has always been around. Binary computers represent everything at a basic level as either a 1 or a 0, and that's what a boolean is. Either true or false, 1 or 0. In the hardware, either a transistor is on or it's off, either a line has a charge or no charge. Everything else is based on that. True and false values have been around as long as binary computers. Even a line like this:IF A\$ = "Y" THEN GOTO 30Either \$A is equal to "Y", or it's not. It's either true, or false. BASIC abstracted out a lot of things, because it's meant for beginners, but you can't really have logic in programming without being able to determine if something is true or not. It's true that some versions of BASIC don't have an actual boolean type that you can save in a variable, but you're still using boolean logic.

##### Share on other sites

IF A\$ = "Y" THEN GOTO 30
yes, that's what i meant, but now one can simply doIF (A\$) THEN GOTO 30was wondering, when "now" began.
##### Share on other sites

It looks like logical if statements showed up in FORTRAN IV in 1961/2, and ALGOL 60 from 1960 had a formal boolean data type. George Boole, the namesake of boolean values, did his work on what would become known as Boolean Algebra in the mid 19th century.