Structs vs Pass-By-Reference


#1

Pass-by-reference was presented as a way to get more than one value out of a function. It seems like structs are also a way to get more than one value out of a function - by creating a function that returns a struct. Is this correct?
If this is correct what is the advantage of using one way over another?


#2

Yes, this is true: you can create functions that return structs, thus returning more than one value.

But, if you do it this way you end up with an incredible number of struct types wandering around. For example, I’d guess that more than 100 methods in the Cocoa Touch libraries use pass-by-reference to return some result type and an error object. So, Apple would have create dozens of struct types just to handle these cases without pass-by-reference.

Excellent question!


#3

Thank you.

I have started reading the next part of the book, introducing objects, and it seems like that is exactly what Apple, and object-orented programming in general, has done. Instead of having a bunch of structs with contained variables though, they are called objects with variables and functions. Is there really a difference between having a bunch of struct types and having a bunch of classes?

By the way, I really love the book. I don’t have a lot to compare it to but it seems to be working.