The p in "return p.weightInKilos / " on pg 66


#1

I tried to ask this question in “What is “p” in Person p?” but nobody was answering. I’ll try here:

Where I am confused is:

“float bodyMassIndex(Person p)
{
return p.weightInKilos / (p.heightInMeters * p.heightInMeters);
}”

If the function parameter is p then why do we need to have “p.weightInKilos” or “p.heightInMeters” or “p.heightInMeters”? p has already been specified in the function argument.


#2

That’s true, but p is an object of type Person and thus has properties.

For example, p.weightInKilos gives the value of a property named weightInKilos in that particular Person object.

If you had this in mind:

float bodyMassIndex (Person *p)
{
      return weightInKilos / (heightInMeters * heightInMeters);
}

It would not compile if the variables weightInKilos, heightInMeters, and heightInMeters were not defined in the current scope.

This:

float weightInKilos;
float heightInMeters;
float bodyMassIndex (Person *p)
{
      return weightInKilos / (heightInMeters * heightInMeters);
}

would compile but would not make any sense because we are passing in a Person object and we are not doing anything with it. (Also the global variables weightInKilos and heightInMeters are presumably being set before a call to bodyMassIndex () occurs.)


#3

I’m not trying to be irritating but the fact that the Person object “p” is passed to this function and is the only object passed at least in this example would seem, logically at least to me, to rule out any other possibility as to what the lines of code within this function (“return weightInKilos / (heightInMeters * heightInMeters);”) are supposed to be working on. It seems redundant to write “p.weightInKilos”, “p.heightInMeters” when “p” has already been called but I guess it’s just one of those “alloc] init]” Objective-C conventions I need to * to myMemory! Thanks for your help. :slight_smile: