Javascript Tutorial - 12 - Parameters in Functions
Top Comments
All Comments (85)
-
awesomeness
-
@MegaTeddy000 In 'alert(add(32, 56))' , we tell JavaScript 'Open a popup box and for the value to be displayed, call the add() function with parameters 32 and 56'. So 'function add()' is read & the two numbers are added. Now assume that there is no 'return x' statement. So the addition of 32 & 56 will surely be performed; but the alert does not have a value to display. If you do write 'return x', the sum of 32 & 56 is calculated and returned i.e. alert( add(32,56) ) = alert(88) !
-
A return statement pretty much returns a value back to, lets say... a variable, so you don't have to re write that variable many times. It's kinda hard to explain i had trouble with it for a long time. The best advice i can give u is to search "java return" or something like that until u get it.
-
i dont understand what return is for......can anyone explaine?
-
hmm this is like byval and byref in VB
-
@supersushi269 more extreme:
function hi(x){
for(i=0;i<x;i++){
document.write("!!!1")
}
}
document.write("OMG");
hi(9001);
lol
-
A real use of functions XD
alert(add(1,add(1,add(1,add(1,
add(1,add(1,add(1,add(1,add(1, add(1,add(1,add(1,0))))))))))) )); TRY IT!
-
@WillsEasyGuitar kk ty
-
@kindofgameplay if you dont tell the function what the return value is, it treats it like a boolean (true or false). giving the variable x, it sends the value of the two numbers added.
-
why
return x; ??
yes, but as the program becomes more complex, there will be more to it than simply adding two numbers. the point of the video is to show how to use parameters in a method that can be used over and over again. This method may contain several lines of code. If you had to use the method 10 times, you'd be saving a lot of time
thebiggerhammer 2 years ago 13
Can someone explain why you don't have to declare the variable x? It makes more sense to me that inside the function you had to state:
var x = a + b;
gorospakabum 10 months ago 3