/    Sign up×
Community /Pin to ProfileBookmark

passing variables between functions

i know this question is relatively easy, but please do help me

i need to pass a variable from two functions, i.e.

function smile(big, happy){
me=happy;
.
.
.}
function sad(){
alert(me);
}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@enderl25Nov 22.2002 — webmark,

to pass a value from one function to another,

pass the value as a parameter of the function call.

as follows:
---------------------------------------------------


function smile () // function header

{

var me; // variable declaration

sad (me); // function call ( with parameter )

return; // return program control to calling function

}

function sad (me) // function header

{

alert (me); // output value to the terminal

return; // return program control to calling function

}
----------------------------------------------------



if you simply need to access a variable from inside of multiple functions,

declare the variable outside of all functions, thus making it a global variable.

then you will be able to access it from with any subsequent funcitons

as follows:
-----------------------------------------------------


var me;

function smile ( )

{

me = 'big';

alert (me);

return;

}

function sad ()

{

me = 'happy';

alert (me);

return;

}
------------------------------------------------------



hope this helps

ritz
Copy linkTweet thisAlerts:
@Beach_BumNov 22.2002 — I think there is a simple answer to your question.

If you have defined a variable, you can use it in as many functions as you want. Its value stays at what ever it was last. You will note that you generally define a variable outside of the function in the first place, then use it in a function. There is no association between a function and a variable per se. So feel free to use a variable in more than one function.

<script . . . .

var whatever

function number1()

...

function number2()

...

</script>

both functions can use variable whatever

Is that what you are asking?
×

Success!

Help @webmark spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...