Jump to content

Javascript: Passing variables into functions question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
atheium

atheium

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
seen below is a bit of code i wrote a few days ago. everything works perfectly fine, however im a bit confused on one bit of it.. the variables cardval and cardt are declared globally, so i understand all functions can use them.

what i dont understand is the () part, and why it wasn't necessary to use. i read just a second ago that variables being passed from one function to another are placed inside the () part of the function.
i didnt include these variables inside the (), so why is my code working?
if it has something to do with them being global, then how do i go about providing information to a function from another function?


function cardvalue(){

cardval=Math.floor(Math.random()*13+1);


if(cardval>=11 || cardval==1){

if(cardval==1){cardval="ace"}

if(cardval==14){cardval="ace"}

if(cardval==13){cardval="king"}

if(cardval==12){cardval="queen"}

if(cardval==11){cardval="jack"}

}

else{cardval=cardval};

return cardval;

}


function cardtype() {

cardt=Math.floor(Math.random()*4+1);

if(cardt==1){cardt="hearts"}

if(cardt==2){cardt="diamonds"}

if(cardt==3){cardt="spades"}

if(cardt==4){cardt="clubs"};

return cardt;

}


function cardtotal() {

cardvalue();

cardtype();

alert("your card is a "+cardval+" of "+cardt);

}


11ism.com <my meaningless empty website, HORAH now with link! (thx gamemaker)

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
You're calling the two prior functions into the third, thus transferring scope of the two into the cardtotal function.

#3
NastyDevil

NastyDevil

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
The functions that have variables declared between parentheses will be able to deal with any variables used to call that function. Let me give you an example: Say you write a function which takes a variable x and multiplies it by 5. You can run that function and it will keep multiplying the same variable by 5 every time it is ran. A function that will be written like this:

function Multip(y)
{
y=y*5;
document.write(y);
}

Will be able to receive any single variable you throw into it when you execute the function and multiply it by 5. So lets say you declared a variable: var bob=5; and then do Multip(bob) the function will first associate y=bob; then multiply y*5 and print out 25. If after that you declared a variable bob2=6; you can still call the same function Multip(bob2) and it will first identify y=bob2; then multiply y=y*5=30 and print out 30 this time.

The function that did not have such a structure will do:
1. var x=5;

execute function:
x=x*5=5*5=25
print: 25

execute again:
x=x*5=25*5=125
print: 125

execute a 3rd time:
x=x*5=125*5=625
print:125




Lol i hope i made it clear....

#4
atheium

atheium

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
thanks to both of you, things are a little clearer, but what if the function has multiple variables inside the () marks? ive seen it work i just dont get how.
11ism.com <my meaningless empty website, HORAH now with link! (thx gamemaker)

#5
NastyDevil

NastyDevil

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
The same way except it receives multiple variables when it is called so you can instead of multiplying y by 5 you can multiply y by x.
EX:
var h=5;
var n=6;
function Multip(myvar1;myvar2)
{
document.write(myvar1*myvar2);
}
Multip(h;n);

What that does is call the function Multip with parameters h and h. It assigns myvar1 the value of h and myvar2 the value of n at the beginning of the execution. It then prints out the multiplication of the two.

#6
atheium

atheium

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
what if the function contained variables separated by comma's?
11ism.com <my meaningless empty website, HORAH now with link! (thx gamemaker)

#7
NastyDevil

NastyDevil

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
yeah its actually commas i just mistyped it.

#8
atheium

atheium

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 298 posts
thanks for your help! im starting to connect the dots so to speak.
11ism.com <my meaningless empty website, HORAH now with link! (thx gamemaker)