Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Whats a difference between a function that is fed something, and a function that

    that is assigned to a variable?

    I found the following in a book,

    Code:
     //  Is the browser internet explorer ?
    var is_ie = browser () ;
    
    //  Display appropriate greeting:  
    ( is_ie) ? greet_ie() : greet_other();
    What is the meaning of the first line? I dont really understand it? Perhaps if it was written like this,

    Code:
     //  Now feed the function:
    
    var whatEver = "whatever"
    var is_ie = browser ( whatEver ) ;
    i could probably then understand it. But i dont really get the meaning of the above.

    The above was an example from a book on the subject of Conditional Operator. Its very short on details.

    In the above, i can understand how the function by the name of browser is being fed something, but i dont understand its meaning in the first example. What in other words will the variable mean? Does it mean that it will now represent the function by the name of browser? Or will it mean something else?
    LovesWar

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    If you something like this:

    var is_ie = browser () ;

    this means the the browser() function returns something to the caller.

    Check the content of the browser() function and you will see some return statements. A function could return any variable type such as boolean, number or anything or no return at all. It depends on how you implement a function.

    By looking at how is_ie variable is used on the second line (in the first set of code you posted), I conclude that the browser() function returns a boolean true or false to check if the browser used is IE or not.

    So the second line uses a ternary operator which is essentially the shortcut of if-else condition to check the value of is_ie.

    This:

    ( is_ie) ? greet_ie() : greet_other();

    is the same with this:
    Code:
    if ( is_ie) {
       greet_ie();
    }
    else{
       greet_other();
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I think, i get it.

    Well, i understood the second part of the code before, i just didnt understand the first part.

    In other words, because a function by its very nature is supposed to do something, you can basically store the result of what it has done in a variable, correct? Thus, if the function was written like this,

    Code:
    function browser()
    {
      //  first create my storage boxes:
    
    var returnValue=true;
    var browser= navigator.appName;
    
      //  Now test one of the variables:
    
      if (browser="Internet Explorer")
      {
        return !returnValue;
      }
      else
      {
        return returnValue;
      }
    }
    then i called it like this,
    Code:
     //  Is the browser internet explorer ?
    var is_ie = browser () ;
    i would be storing either a true, or false, value inside the variable is_ie, correct? Or did i miss the bull's eye?

    I think i understand what you meant by calling a function. I dont necessarily have to feed a function anything in order for it to do something. Its only when i want for a function to affect a particular variable, or a value, in most cases, do i feed the function something. Otherwise, all i would do is basically attach a function to a variable in order to store the result of function, correct?
    LovesWar

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by SpiritualStorms
    i would be storing either a true, or false, value inside the variable is_ie, correct? Or did i miss the bull's eye?
    Bull's eye!

    Quote Originally Posted by SpiritualStorms
    I think i understand what you meant by calling a function. I dont necessarily have to feed a function anything in order for it to do something. Its only when i want for a function to affect a particular variable, or a value, in most cases, do i feed the function something. Otherwise, all i would do is basically attach a function to a variable in order to store the result of function, correct?
    See this sample:
    Code:
    function addThis(x, y){
      return x+y;
    }
    var sum = addThis(1, 2);
    alert(sum); //alerts 3
    
    function addThis(){
      return prompt("What's your name?","Type your name here");
    }
    var myname = getName();
    alert(myname); //alerts whatever name was entered
    
    function sayHi(){
       alert("Hi!");
    }
    sayHi();
    
    function saySomething(str){
       alert(str);
    }
    saySomething('Hello!');
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    did you mean the first, or the second script?

    I was looking at your samples, and it dawned on me that perhaps you made a typ0?

    Here's what you have:

    Code:
    function addThis(){
      return prompt("What's your name?","Type your name here");
    }
    var myname = getName();
    alert(myname); //alerts whatever name was entered
    But i wonder if you meant this,

    Code:
    function getName()  {
      return prompt("What's your name?","Type your name here");
    }
    var myname = getName() ;
    alert(myname); //alerts whatever name was entered
    Is that what you meant?
    LovesWar

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Yes, that was a typo. Copy-Paste syndrome...
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts

    np, i sort of thought so

    I was wondering though, do global variables work differently than local variables over, and beyond thier life span? i ask, because i have the following function, but doesnt seem to work:

    Code:
    function addWindow()
    {
    var fox;
    var fileAddy = "Adding.html";
    windowName = "Math_Window_add";
    
    var features;
    
    features = "width=440px,height=150px,"
    features+="top=300px,left=300px"
    
    fox=window.open(fileAddy,windName,features);
    
    fox.focus();
    }
    The function does not work. And i am wondering as to why?
    LovesWar

  • #8
    Regular Coder
    Join Date
    Jun 2004
    Location
    underground
    Posts
    186
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #9
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to make sure i am understanding this right: If a function has a return statement inside, does that mean the function will not work without it? Or is there a much more specific purpose for why return statements are included inside a function? I have the following:

    Code:
    function greeter()
    {
      var visName   ;
    
      visName   = prompt("yada yada yada") ;
    
      return visName  ;
    }
    I would want to know why a return statement would be needed, or not.
    LovesWar

  • #10
    JPM
    JPM is offline
    Regular Coder
    Join Date
    Mar 2004
    Location
    Norway
    Posts
    204
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In many cases you dont need a return statment. Say if you have function that opens a window, it wouldn't need to return anything, just open the window. But if you have a function that turns a string into lowercase than that function would have to return the final string.
    <JPM />

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SpiritualStorms
    I would want to know why a return statement would be needed, or not.
    Ok, here's an example: Imagine you know someone who can calculate the sum of the prices of two products, say bananas and apples. You can call him on the phone and say him the number of bananas and apples you want to buy, and he caculates the summed up price. Now you call him and say: "2 bananas, 3 apples". You wait, but you don't here an answer. You ask: "Where's my answer?" and the other replies: "I stored it somewhere in my mind.". What would you do - you'd ask: "Can't you return the answer directly to me instead?"

    It's the same way with functions. It depends on the type of use for the function. If it is expected to return a result, then you need the return statement to explicitly return a calculated value, or whatever the result may be. The example above in JavaScript:

    Code:
    function calculateSum(bananas, apples) {
    	return bananas * 0.25 + apples * 0.5;
    }
    
    var expense = calculateSum(2, 3);
    The advantage is that you can call the function wherever you want and store the result in new variables. If the function would store the result in a global variable, it would always overwrite the one from a former call.

    But basically it's up to the programmer to decide if a return statement should be used, and what it should return. There are many different usage patterns for returned values: They can be calculated results, modified parameters, undefined, true/false for error checking... it really depends on the specific task the function is built to do.
    De gustibus non est disputandum.

  • #12
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think i understand, sort of. So in a sense, if you want to know the answer, and not so much if the answer is being performed that you would use the return statement. In other words, i could have a function that adds, and it would do its adding, but doing its adding, would not necessarily tell me what the result is, correct? In this sense, then, if i want to know what the result is, in addition, to the rendering of the result, i would have to include a return statement?

    But whether or not, i include a return statement, a function could still do its job, correct?
    Last edited by SpiritualStorms; 07-15-2004 at 08:06 PM.
    LovesWar


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •