# 28. Passing Objects into Functions Question

#1

Hey folks! This isn't a help question, so much as it's something I'm trying to understand. I got the correct code, so that isn't the problem. I'm just wondering how the ageDifference function determines who person1 and person 2 are.

``````// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
};

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function
var diff = ageDifference(alice, billy);``````

If I run the code, it runs correctly. However, I have no idea why it knows that person 1 is Alice, and person2 is Billy. Does it have to do with the order that Alice and Billy are created? For example, if Billy was created first, does that make him person1 in the function? Basically I'm just trying to understand how the code knows that Alice = person1 and Billy = person2.

#2

yea, the order is important. If you have this:

``var diff = ageDifference(alice, billy);``

alice will be person1, and billy person2, if you swap it:

``var diff = ageDifference(billy, alice);``

billy will be person1, and alice person2

If you would do this:

``var diff = ageDifference(billy);``

you will get an error, given there aren't enough arguments (1) to satisfy 2 function parameters

#3

Ahhh! That makes perfect sense. I forgot that when I called the function, the parameters of alice and billy determined who was person1 and person 2. Now I completely understand. Thank you so much!

#4

person1 and person2 parameters are just placeholders until you call the function, then the parameters actually get values

#5

Yep! That is exactly what I forgot. Thank you again.

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.