Greetings!

I'll plead ignorance...that's easy. :-/ This is from: http://www.w3schools.com/js/js_functions.asp

function product(a,b)
{
return a*b;
}

Without assigning a variable to the function, how does the correct value get returned? Or even interpreted?

[pseudocode]
a*b=c
return c
[/pseudocode]

2nd Q:

Is this "undeclared (unseen) variable" local or global?

Thanks, in advance, for your time!

< Steve >

Recommended Answers

All 8 Replies

Hi seblake,

There are simple 2 things about a function:-
1. Defining the function
2. Calling the function

When you 'Define' a function (as you did in above), it does nothing. Defining means you are just writing logic of the problem to solve in the function. In your case you have defined a function which will take 2 parameters (a, b) and will return (a*b). This function is useless until you 'Call' it.

To actually execute a function you need to 'call' it with parameters. In above case it will be called as:-

var c = product(2, 4);

Here a=2 and b=4 and the product function return (a*b) ie (2*4) and the result will be stored in c. After executing of above line. c will contain product of 2 and 4.

Hope I gave you the answers.

Thanks, Luckychap.

Let me try to clarify a bit. The first routine which includes "return a*b" was from the link I provided. That is the basis for my questions.

The "a*b=c" and "return c" is from my antiquated knowledge of how functions used to be written. Could you please explain the syntax of the function as it was presented from the w3schools tutorial?

Thanks!!

< Steve >

Steve,

function product(a,b)
{
  return a*b;
}

is equivalent to

function product(a,b)
{
  var c = a*b;
  return c;
}

And there's nothing to prevent you writing the function that way.

The line var c = a*b; works as follows:

  1. performs the calculation and stores the result in a register
  2. creates a local symbol c
  3. associates c with the stored result

Then the line return c; causes an anonymous reference to the stored result to be returned, as indicated by the symbol c , but not the the symbol itself.

In your original version of the function, the line return a*b; ,

  1. performs the calculation and stores the result in a register
  2. returns an anonymous reference to the stored result.

With this understanding, you should be able to see that the symbol c is not necessary. It's just something internal to the function, which may be useful in a more extensive function where the symbol is available for later use further on in the function's code.

Airshow

as mentioned in w3schools:

// Syntax
function functionname(var1,var2,...,varX)
{
    //some code
}


// Real function
function add(a, b) {
    var c = a+b;
    return c;
}

1. To function should have a name in our case its 'add' and the name should probably explain what that function does. You can use different name as well like 'sum', 'plus', 'addNumbers' or even 'foo'. Its totally up to you to decide. But its always good to give function name similar to what they are suppose to do.

2. The function can have(not always) input arguments on which it will do some task. Like in our case we want to add a, b. Here 'a' and 'b' are 2 input arguments for 'add' function. 'a' and 'b' can be any number.
And you can also have functions with no input arguments like:-
NOTE: these input arguments are local to that function they are alive in function scope once the function has finished executing these variables will be gone.

function foo() {
   alert('Hi, whats up!!!')
}

When this function is called, it will show a popup with message 'Hi, whats up!!!', thats it, nothing fancy, no logic, no return value.

3. When you want a function to perform some task on input arguments you need to add some code/logic in the function.

function add(a, b) {
    var c = a + b;
}

Here we want to add 'a' and 'b' and store the result in 'c'.

4. Once the function have done the task, it will return automatically back from where it was called. If you want you can return some value with the function. Like sum of 'a' and 'b'.

// This function will only add 'a' and 'b'
function add(a, b){
    var c = a+b;
} 

// This function will add 'a' and 'b' and also return the result. 
function addAndReturn(a, b) {
    var c = a + b;
    return c;
}

// Test above two functions
var sum = add(2, 8);
alert("sum: " + sum); // here 'sum' will be undefined as 'add' is not returning anything

var sum1 = addAndReturn(10, 2);
alert("sum1: " + sum1); // Here 'sum1' will contain 12 as 'addAndReturn' is returning the 'c'

Busy weekend - sorry for the delay.

Many thanks to Airshow and Luckychap. I really appreciate you guys using crayons for your explanations!

OK - so a function does not have to return anything... I just can't seem to get my head around calling a function and not returning the result.

re: Airshow -

How would the code/software use or reference the "a*b" later in the program if you do not assign it to a variable? I believe the compiler needs a token in order for the resultant value to be used later... or... when it's on the stack, how do you remember how many times to pop off the other values to get to the one you want to use, i.e., "a*b"?

re: Luckychap -

The only difference I can see in a direct comparison is that "function add" returns an immediate result - to the screen; wherein, "function addAndReturn" not only provides an immediate result - to the screen, but also wants to use the value later in the program because you are assigning the result to "c". Right?

Really do appreciate the time!!

< Steve >

Function 'add' only returns the control to next execution line where as 'addAndReturn' returns the control to the next line as well as value of a+b.

If you you are intending not to store the value return by 'addAndReturn' then there is no difference between 'add' and 'addAndReturn'.

As I told you, all the variables local to the function including arguments will be gone after function finishes execution. You have only one thing left that is return value of the function. And its always good to return some value from function to get the status of what happened in the function after execution.

For example:

function riskyfunction(x, y) {
    try {
        /// Do somthing risky. 
        /// And guess what, it executed perfectly
        /// Tell the caller function - All is well
        return "Success";
    } catch(e) {
        /// damn somthing went wrong.
        /// Tell the caller function - sorry! I failed to execute, do something other than me
        return "Failure";
    }
} 


// Testing by calling riskyFunction
var status = riskyFunction(10, 0);
if(status == "Success") {
     /// Hurray!! You are the man
} else {
    /// :( ... new ways ... lets try another function
    goodFunction(10, 0);
}

GREAT!!! I do get it!!!

Thank you very much for your time and patience.

< Steve >

Steve,

re: Airshow -

How would the code/software use or reference the "a*b" later in the program if you do not assign it to a variable? I believe the compiler needs a token in order for the resultant value to be used later... or... when it's on the stack, how do you remember how many times to pop off the other values to get to the one you want to use, i.e., "a*b"?

My statement could have been better worded. Here it is again with some mods to make it clearer.

  • With this understanding, you should be able to see that the symbol c is not necessary. If c were set, it would be something internal to the function, which may be useful in a more extensive function where the symbol (c) would be available for later use further on in the function's code.

Airshow

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.