0

OK lets take an example:

function student(name,age){
document.write("I am " + name + "and I am " + age + " years old." );
}
var student_name = "Divyansh";
var student_age = 17;
student(student_name,student_age);

// now my question is what do we call the arguments of the function student :

(a) is it the variable: student_name & student_age respectively,

(b) or is it the value contained by the variables: divyansh & 17 respectively

PS- I have also posted the same question on codechef, but it seems nobody is interested in giving answer to my stupid and childish question. But, what do i do i am trying to study Javascript myself by the book head first javascript programming and got strucked due to this doubt in one of the question, Please Help.

ale
  • 10,012
  • 5
  • 40
  • 49
  • a variable is value container(in your case, because your vars doesn't contains object reference), so ==> b for your reference http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language – ale May 11 '15 at 11:04

5 Answers5

0

An argument of a function is an expression with which the function is called.

In this case, the function is called with two expressions as arguments: student_name and student_age. These expressions yield a value when evaluated: the value of the variables.

0

The function is called with two parameters. These parameters are the variables. And the function get the values of those variables and assign it in its local variable namely 'name' and 'age'.

0

Have you used console.log() method? Have you ever wondered what's the maximum number of parameters it takes? Answer is as many as it can.

Lets take an example.

function print(x,y){
  console.log(x);
  console.log(y);
}

Well this is an easy function, takes 2 arguments and print them in the console.

Lets say I have 5 parameters passed to function print. a,b,c,d,e. How would you modify the print method? May be something like this. (Don't write programs like this. It ain't effective).

function print(a,b,c,d,e){
  console.log(a);
  console.log(b);
  console.log(c);
  console.log(d);
  console.log(e);
}

How about if I pass 100 parameters like, a1, a2, ... a100. What if I pass 1000 parameters. You can't write 1000 console.log statements to print them. So you would think of a for loop to print all the parameters.

But wait!

How do I access all the parameters inside a for loop unless if they are an array (use for-loop or for-in to iterate) or an object (use for-in to iterate)?

Now comes the object "arguments". This is a built-in object implicitly created by javascript for every function you declare.

What does this store? It stores all the parameters you passed to the function in the form of object.

arguments { 0: a, 1: b, 2: c, 3: d, 4: e };

Now I have something like an object that holds all the parameters passed to the function, it will be easier to access them in a for-in loop. Now my print method will look like:

function print(a,b,c,d,e,f) {
  for (var prop in arguments) {
    console.log(arguments[prop]);
  }
}

This will loop through all the properties of the arguments object and prints the value stored in each property.

What is stored in each property of argument object?

fn parameter object-property value stored in property

a                  0             value of parameter a
b                  1             value of parameter b
c                  2             value of parameter c
d                  3             value of parameter d
e                  4             value of parameter e
f                  5             value of parameter f
Harish
  • 616
  • 6
  • 11
  • You explain what the built-in `arguments` variable is and how to use it, but I don't think the question is about that. – Siguza May 11 '15 at 11:46
  • @siguza What do you think the question is? – Harish May 11 '15 at 11:55
  • Well, see the first couple of lines from [my answer](http://stackoverflow.com/a/30166714/2302862). But OP's phrasing is really ambiguous apparently. – Siguza May 11 '15 at 13:21
0

If I understood your question correctly, you want to know whether you should say

I called student with student_name and student_age.

or

I called student with "Divyansh" and 17.

right?

Well, that depends on the whole context, and sometimes it's possible to use both.
And this is to some extent a question of opinion, but I'll try to justify my opinion as good as I can.

I believe you should refer to the arguments by their names (i.e. student_name and student_age if

  • their value is of no significance for what you're saying.
    Unnecessary information usually leads to confusion.
  • their value is unknown.
  • their value is long and you previously mentioned it.
    Long values such as Math.pow((5*x+7*y-35*v*x*z)/(42*m/35*k+47*f+99*l), 35*22*88*k) tend to make written text messy and are really annoying to speak out many times in a row, so you'll want to refer to them by name.

You should refer to the arguments by their value when their value matters, e.g. when you have a bug that only occurs when certain values are passed to your function, but not some others.

Now there is a gap between those, where I believe you can use both.
For that case, I'd apply the golden rule
As short as possible, but as long as required.

If you had the following code:

var name = "Divyansh";
console.log(name);
var name = "Marc";
console.log(name);
var name = "Jon Skeet";
console.log(name);

Then I believe all of the following statements are perfectly valid:

I called console.log three times with name as argument.

or

I called console.log three times with "Divyansh", "Marc" and "Jon Skeet" as argument.

But it really depends on how much information you need or want to give.

Siguza
  • 21,155
  • 6
  • 52
  • 89
-2

In your case you are passing two arguments named student_name and student_age to student function which contains value Divyansh and 17 respectively.

Vivek Gupta
  • 955
  • 4
  • 7