So you’re a C/C++ programmer and want to enter the wonderful world of Objective C. Well you have come to the right place for that. This tutorial will cover some of the differences between the language of C and Objective C for functions. I will add subsequent tutorials for the other parts later. A lot of it is straight forward but some might seem like glass candy. So let’s jump right into it.
Functions:
Functions in Objective C tend to be quite different syntactically than in C/C++. The general concepts are still the same though.
1. declaration
- (returnType) functionName:(paramType)paramName paramIdentifier:(paramType2)paramName2;
Above is an generic function declaration in Objective C. It might seem a little confusing. especially when you get to function parameters, but we will take this thing apart piece by piece.
- (returnType) functionName:(paramType)paramName paramIdentifier:(paramType2)paramName2;
The first thing to notice is the “-” at the beginning. This says that the function will be an instance method. A class method would be denoted with a “+” instead.
- (returnType) functionName:(paramType)paramName paramIdentifier:(paramType2)paramName2;
Next comes the return type. In objective C you must contain it within parentheses. Pretty simple…
- (returnType) functionName:(paramType)paramName paramIdentifier:(paramType2)paramName2;
Next the function name which is the same as in C.
- (returnType) functionName:(paramType)paramName paramIdentifier:(paramType2)paramName2;
Here is where things get a little tricky. The colon after the function name denotes that a parameter is coming up next. The type is contained in parentheses and then the name comes after.
- (returnType) functionName:(paramType)paramName paramIdentifier:(paramType2)paramName2;
Subsequent parameters use a space as a seperater instead of a comma and have a parameter identifier. This is used for when you call a function which will be explained later. Then like the first parameter the colon and parameter type and parameter name follow.
Make note that the parameter identifier for the first parameter will be the function name. So, it is sometimes useful to add information on the end of the function name. For example, you could say functionNameWithParameter as the function name.
NOTE:
But what if our functions don’t have parameters? Well this is very simple, but can trip C programmers up. Since we don’t use parentheses to house our parameters there is no need to add the obligatory “()” at the end of the function as we would in C. The function without parameters is just,
- (returnType) functionName;
2. Definition
Alright, function declarations down, let’s move on to definitions. You will be surprised with the differences between the declaration and definition.
- (returnType) functionName:(paramType)paramName paramIdentifier:(paramType2)paramName2
{
//your code here
}
Above is a generic function definition in Objective C. Wait, there is nothing different here at all. Surprise, it’s exactly like the declaration except you have curly brackets and your code just like you would in C.
Again, something to note is that paramIdentifier is only used when calling a function. If you wanted to reference a parameter inside your function you would use paramName or paramName2.
3. Calling functions
Unfortunately there are some changes between calling functions in c and calling functions in objective c.
[classObject functionName:param1 paramIdentifier:param2];
Above is a generic function call in Objective C. Well, that is quite different. Lets pick this sucker apart.
[classObject functionName:param1 paramIdentifier:param2];
The first thing to take note of is that you will always be surrounding your function call with square brackets.
[classObject functionName:param1 paramIdentifier:param2];
Second is the object. Since we are working with class structures we need to know where this function is located that we are calling.
NOTE:
If you are calling the function inside of the class you instantiated it in then you would use the objective c object “self”.
[object functionName:param1 paramIdentifier:param2];
Next we come up to the function name which is just like the declaration.
[object functionName:param1 paramIdentifier:param2];
Here is that colon that we saw after the function name in the declaration. It also denotes that a parameter is coming up. This is like C in that you put the parameter you want to pass here.
[object functionName:param1 paramIdentifier:param2];
Now here is that parameter identifier I mentioned earlier. This is used to help the caller of the function know what to put there. After the colon you will again place your parameter.
Conclusion
There might be other quirks that arent mentioned here, but overall that is the gist of functions in objective c. I hope this tutorial helped, and feel free to give me suggestions (especially since this is my first tutorial) or let me know if there are corrections that need to be made.
Patrick