function ty(pcheck) {
document.write("I want "+pcheck);
}
var pcash = 500; 
ty(pcheck);

Can the outside variable have a different name and the function still call it ? I read it can, so when I attempt it, it doesn't work !

function hu () {
var one = "today";
var two = "tomorrow";
var total = one+two;
return (total);
}
hu()
document.write(total);

If return, returns the result outside of the function, how come I can't do a write of the return ?

In your first code snippet, on line 5, you're calling ty(pcheck), but 'pcheck' hasn't been defined. Did you mean to call ty(pcash)?

In the second code snippet, function 'hu' will return "todaytomorrow". You should find it'll work if you change line 8 to document.write(hu()); Line 7 can be removed as it's not doing anything, and is missing an ending semicolon.

That's how you return a function, call the function, thanks :)

Look at this example;

function check_alert (pcheck) {
window.alert("You make $"+pcheck);
}
var paycheck=1200;
check_alert(paycheck);

function ty(pcheck) {
document.write("I want "+pcheck);
}
var pcash = 500;
ty(pcheck);

Why are you using document.write - is supporting Netscape 4 and earlier users still important for your site. All browsers more modern than that have better ways of outputting to the page that work after the page has loaded without creating an entire new page. The simplest one is innerHTML.

Member Avatar for diafol

Ok, this is what you have...

function check_alert (pcheck) {
    window.alert("You make $"+pcheck);
}

function ty(pcheck) {
    document.write("I want "+pcheck);
}

var paycheck=1200;
check_alert(paycheck);

var pcash = 500;
ty(pcheck);

Just 'alert' should be OK?
Anyway, pcheck does not exist outside the scope of the check_alert function. ALso pcash isn't doing anything.
the call to ty(pcheck) should fail because, as mentioned, pcheck does not exist here. What are you trying to do??

function check_alert (pcheck) {
   alert("You make $"+pcheck);
}

function ty(pcheck) {
    return ("I want "+pcheck);
}

var paycheck=1200;
var pcheck = check_alert(paycheck);

console.log(ty(paycheck));

That makes more sense to me.

Ah !

Since the outside variable and the parameter share the same name the outside variable is sending the value of paycheck to the parameter, little bit of a mental shake up, I hope I understand and I hope what I wrote was understood :)

Member Avatar for diafol

OK, just so you're aware, the parameter named in the function itself is usually only used for internal stuff within that function (e.g. 'content'):

var globalContent = "Any old stuff";

function myFunc(content)
{
    bigContent = content + " and more content";
    return bigContent;
}

var newContent = myFunc(globalContent);

console.log(newContent); //gives: Any old stuff more and more content
console.log(globalContent); //gives: Any old stuff (unchanged)
console.log(content); //Undefined
console.log(bigContent); //Undefined

However...

var globalContent = "Any old stuff";

function myFunc(content)
{
    bigContent = content + " and more content";
    globalContent = "Any new stuff";
    return bigContent;
}

var newContent = myFunc(globalContent);

console.log(newContent); //gives: Any old stuff more and more content
console.log(globalContent); //gives: Any new stuff (changed)
console.log(content); //Undefined
console.log(bigContent); //Undefined

Hope that's clear.

Where did you define bigContent, or is that a variable ?

Member Avatar for diafol

That's the point. bigContent is declared inside the function, so it does not exist outside. So it gives 'undefined' when you try to do something with it outside myFunc function.

Really I should have placed 'var' in front of it I suppose.

Since the variable bigContent was not declared outside the function it's undefined but content is define therefore it's usable code, all the while ignoring bigContent=

Member Avatar for diafol

No 'content' is not defined outside the function - it's a parameter of the function - so does not exist outside the confines of the function itself.

function myFunc(content)
{
    var bigContent = content + " and more content";
    globalContent = "Any new stuff";
    return bigContent;
}

content does not exist
bigContent does not exist
globalContent does exist because it is declared outside the function (see previous post)

Didn't reply earlier to this, crazy life !

The function is told to execute by reading the parameters from the variable "globalContent" while executing the function in which content is put into the variable bigContent and has the string added ! Since you are writing the variable newContent.

Member Avatar for diafol

Which code are you commenting on - and is this a question?

I'm commenting on this code, just as an explination on how the code works :)

var globalContent = "Any old stuff";
function myFunc(content)
{
bigContent = content + " and more content";
return bigContent;
}
var newContent = myFunc(globalContent);
console.log(newContent); //gives: Any old stuff more and more content
console.log(globalContent); //gives: Any old stuff (unchanged)
console.log(content); //Undefined
console.log(bigContent); //Undefined
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.