WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Javascript - Function parameters Thu Mar 22, 2012 3:06 am

Mr.Joker

Mr.Joker
Mr. WebArtz
Mr. WebArtz
Javascript - Function parameters
Working with function parameters in javascript


Hello once more, I am Mr.Joker and today I will show you how to use parameters in funciton. So what is parameter? Well, imagine that parameter is some variable with a value. If you store a variable without a value, you can asign some value to that parameter. And better we go to examples. Everyone learn from examples.

Code:
<script type="text/javscript">
function troll(x) {
alert("I love " + x)
}
troll("WebArtz");
</script>
So what just I did? I add some variable inside brackets. But as you see I never add a value to that variable. And what that function does? That funtion alert popupbox with text "I love" plus variable. But problem is we dont have a value of variable. What now? Well its simple. Just add name of function in code after function and inside brackets add value. That value will automaticly mean its eaqul to variable x. So when we run this code there will be popup box with text: "I love WebArtz." . Quite simple, isnt it? Lets see this example:
Code:
<script type="text/javascript">
function troll(x) {
var x = "WebArtz";
alert("I love " + x)
}
troll();
</script>
Here the result will be same while runing the code but what is different? Well I add a variable inside function as parameter, but i didnt add a value after like troll("WebArtz"), no, I add a value inside a function for variable x and thats why result is same.
Ok here is example with two parameters:
Code:
<script type="text/javascript">
function troll(x,y) {
alert(x + " is larger then " + y)
}
troll(6,3);
</script>
What we have here? We just have one more parameter. But the princip is totaly same. We just add some job that function should do, and then add a value to those variables like i did: troll(6,3). There is other way of course, like this:
Code:
<script type="text/javascript">
function troll(x,y) {
var x = 6;
var y = 3;
alert(x + " is larger then " + y)
}
troll();
</script>
As you can see, its really simple to use parameters. But should i give you more advanced example? Of course i should. Imagine we have a task to make code that will compare values that user enter and then say which word is longer. How to do that? Here is code i writen:
Code:
<script type="text/javascript">
function troll(x,y) {
var x = window.prompt("Enter first word:");
var y = window.prompt("Enter second word:");
if (x.length>y.length)
alert(x + " is longer word then " + y);
 else if(x.length<y.length)
alert(y + " is longer word then " + x);
else
alert("Both words have equal number of charaters.")
}
troll();
</script>
How does it work? Well, lets see. I have two parameters inside function. Then i write a code that variable x have a value that user enter by prompt box, and do the same for y variable. After user enter values those are stored inside x and y variables. Then i make if&else condition. I say, if x.length(when you add .length to a variable those variable characters are converted to a integer, so if i enter test its not test for script its 4 because it becomes integer. It count how many characters that word have). So i made condition if first word is larger then second one to write a message that ensure that. Then i made else if condition if y if second word is longer then first one to print result to our user. And a else condition say if those numbers are equal what to say. Its totaly simple. Hope you like my tutorials. See ya later on Very Happy

Notice : These tutorials are copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

2Accespted Re: Javascript - Function parameters Thu Mar 22, 2012 9:06 am

ankillien

ankillien
Administrator
Administrator
Nice tutorial Very Happy

Accepted

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum