Home / Software / Computer programming / Javascript function help
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] - (Bottom) Print
Author Topic: Javascript function help  (Read 507 times)
Google
Topic Starter
Adviser



Thanked: 2
Posts: 997

AC Milan #80

« on: November 21, 2009, 05:27:27 PM »

So I am doing this tutorial on w3schools, and this is the code:
Code: [Select]
<html>
<head>
<script type="text/javascript">
function product(a,b)
{
return a*b;
}
</script>
</head>

<body>
<script type="text/javascript">
document.write(product(4,3));
</script>

<p>The script in the body section calls a function with two parameters (4 and 3).</p>
<p>The function will return the product of these two parameters.</p>
</body>
</html>


What I don't understand is in this line;
Code: [Select]
function product(a,b)
Why do the variables have to be in the parameters, when the operation is being declared here;
Code: [Select]
return a*b;
And the values of the variables are declared here:
Code: [Select]
document.write(product(4,3));
Like, why are the variables there twice? I mean does it really need to be? Is the first one just declaring the values, and then the code in the body is giving them their values?

IP logged

BC_Programmer
Mastermind


Thanked: 682
Posts: 15,624

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #1 on: November 21, 2009, 11:01:57 PM »

the function line is a declaration.

a,b are defined as parameters.

if they were, for example, ValA and ValB, they would be referred to as such in the function body. otherwise how would the interpreter know that a*b were the parameters passed rather then new variables being defined?


The values of the variables are not "declared". what you refer to is the function call. the function can be called repeatedly with different parameters, and the parameters passed and their values are used within the function through the parameter names.
IP logged

Google
Topic Starter
Adviser



Thanked: 2
Posts: 997

AC Milan #80

« Reply #2 on: November 22, 2009, 08:47:29 AM »

Okay so the variables have to be declared before you can use them in an operation like that?
IP logged

BC_Programmer
Mastermind


Thanked: 682
Posts: 15,624

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« Reply #3 on: November 22, 2009, 01:28:21 PM »

Okay so the variables have to be declared before you can use them in an operation like that?

No- your missing what I mean.

If you had something like this:

Code: [Select]
var a, b;
a=34;
b=12;
c=a*b;

the "var" line is unneeded; it will work either way. However, if you have a function like that you described above, but without parameters:

Code: [Select]
function product()
{
    return a*b;
}

then it would be necessary to assign values to a and b before calling the function.
Code: [Select]
a=13;
b=5;
c=product();

the "definitions" as you call them, in the parameter list of the function, tell the calling procedure how many arguments can be passed AND also tells the interpreter what they are to be referred to as in the function body.
IP logged

Pages: [1] - (Top) Print 
Home / Software / Computer programming / Javascript function help « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.085 seconds with 21 queries.