...

View Full Version : newbie question: variable and function



hello20109876
05-06-2006, 09:15 AM
see the piece of code:
-----------------------------------
<html>
<head>
<script language="JavaScript">
function makeText(var txt,var pic)
{
var s=txt+", hello, "+pic;
return s;
}
</script>
</head>
<body>
*********
<br>
<script language="JavaScript">
var str=makeText("More Text","down.bmp");
document.write(str);
</script>
<br>
</body>
</html>
-------------------------

my questions:

1)why function above can not return that string?
can java-script return a value?

2)are variables in java-script similar to c++ which contain following concepts:
global, constant,reference,pointer variable etc?

3)are there any differences if codes are in one <script ..></script> pair or in 2 <script ..></script> pairs?

BTW: I am a c++/java program, please compare with c++/java if possible.

boxxer03
05-06-2006, 11:54 AM
It won't return a string because your not asking it to return a string, or at least asking it to write it out. This is your code with some corrections and explanations. Granted this may not be the most efficient way, it'll be easier for you to understand because you wrote it.


<script type="text/javascript">
<!-- Use script type='text/javascript' because its w3c compliant and the 'correct' way to write it -->
function makeText(txt,pic)
<!-- You don't need the word 'var' in front of 'txt' and 'pic' because its already understood that they are variables -->
{
var s=txt+", hello, "+pic;
document.write(s);
<!-- Instead of using return s; which you can use to pass avraible from function to function, its easier to use the document.write right there so it will go ahead and write it wherever you place the code to call it -->
}
</script>

Now for your 'calling code' (because your calling the function above up):

<script type="text/javascript">
<!-- Use script type='text/javascript' because its w3c compliant and the 'correct' way to write it -->
makeText("More Text","down.bmp");
<!-- Since your calling the function up to be placed here you don't use variable to call it, just the name of the function and the variables inside the brackets -->
</script>

And you could just place that last code anywhere on the page that you want that text to be displayed. But there are exceptions to the rules, mroe efficient ways of doing things, etc... But its a learning process and you gotta start somewhere. (Just trying to cover my butt because someone usually says that you could do this better by doing this.......whatever......) I'm just trying to give you an idea of how you can do it. I would put it in C++/Java terms btu I have no idea about either of those, so if you have any questions just ask.

hello20109876
05-06-2006, 01:10 PM
many thanks for your long reply.

acturally, after several funny tests, I have solved the problem myself:
result is:
if i add var before parameters of txt and pic, the function doesn't work.

very strange, i don't understand why java-script is designed in this way - I have to just follow it.

:D
:confused:
:rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum