Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSam Matthews
8,276 PointsDifference between function calls with arrays
Going through this course I notice that the functions seem to be inconsistent in the way they are called. So for example:
todo.removeAtIndex(2) todo.insert("Call Mumma", atIndex: 0)
removeAtIndex says I only need to specify the index position, where the insert function says I need the value I'm inserting into the array and then this:
atIndex: 0
Why do I need to specify the name of the parameter in the 2nd part of the call, but not the first ?
Is this some sort of standard that is adopted in Swift or is the second example a method call as opposed to a function.
In my world (oracle dba who is somewhat familiar with oracle database and PL/SQL), this is unusual, so if anyone has any reasons why this would be then that would be great.
I always like to understand why things are done a certain way.
Many thanks
Sam.,
1 Answer
Rodrigo Chousal
16,009 PointsHey Sam!
I'm not entirely sure, but I believe the reason for the parameters to be different is simply because you don't need to know the value of the index in order to remove it. For example, you can have an array with 5 values, and simply remove index[1] without knowing the value of that index. On the other hand, when inserting a new index, that index needs a value and a location, that is why you need to specify the value, and then where you want that value to be located inside the array (the index).
Hope this helps!
karan patel
1,046 Pointskaran patel
1,046 Pointsfunctions are methods arrays are ordered list of data entered func xyz() { //some kind of method .... .... .... }
var abc = ["LOL","ROFL","COOL","BATMAN","TREEHOUSE"]