Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [11]
When should I use a function?
Suppose you are writing a program to congratulate students for completing a Big Nerd Ranch course. Before worrying about retrieving the student list from a database or about printing certificates on spiffy Big Nerd Ranch paper, you want to experiment with the message that will be printed on the certificates.
To do that experiment, create a new project: a C Command Line Tool named ClassCertificates.
Your first thought in writing this program might be:
int main (int argc, const char * argv[])
{
printf("Mark has done as much Cocoa Programming as I could fit into 5 days\n");
printf("Bo has done as much Objective-C Programming as I could fit into 2 days\n");
printf("Mike has done as much Python Programming as I could fit into 5 days\n");
printf("Ted has done as much iOS Programming as I could fit into 5 days\n");
return 0;
}
Does the thought of typing all this in bother you? Does it seem annoyingly repetitive? If so, you have the makings of an excellent programmer. When you find yourself repeating work that is very similar in nature (in this case, the words in the printf statement), you want to start thinking about a function as a better way of accomplishing the same task.
How do I write and use a function?
Now that you’ve realized that you need a function, you need to write one. Open main.c in your ClassCertificates project and add a new function before the main function. Name this function congratulateStudent.
#include void congratulateStudent(char *student, char *course, int numDays) { printf("%s has done as much %s Programming as I could fit into %d days.\n", student, course, numDays); } (Wondering what the %s and %d mean? Hold on for now; we’ll talk about those in the next chapter.) Now edit main to use your new function: int main (int argc, const char * argv[]) { congratulateStudent("Mark", "Cocoa", 5); congratulateStudent("Bo", "Objective-C", 2); congratulateStudent("Mike", "Python", 5); congratulateStudent("Ted", "iOS", 5); return 0; } Build and run the program. You will probably get a warning marked by an exclamation point inside a small yellow triangle. A warning in Xcode will not prevent your program from running; it just draws your attention to a possible problem. The text of the warning is to the right of the code. This warning says something like No previous prototype for function 'congratulateStudent'. Ignore this warning for now, and we’ll come back to it at the end of this section. Find your output in the log navigator. It should be identical to what you would have seen if you had typed in everything yourself. Mark has done as much Cocoa Programming as I could fit into 5 days. Bo has done as much Objective-C Programming as I could fit into 2 days. Mike has done as much Python Programming as I could fit into 5 days. Ted has done as much iOS Programming as I could fit into 5 days. Think about what you have done here. You noticed a repetitive pattern. You took all the shared characteristics of the problem (the repetitive text) and moved them into a separate function. That left the differences (student name, course name, number of days). You handled those differences by adding three parameters to the function. Let’s look again at the line where you name the function. void congratulateStudent(char *student, char *course, int numDays) Each parameter has two parts: the type of data the argument represents and the name of the parameter. Parameters are separated by commas and placed in parentheses to the right of the name of the function. What about the void to the left of our function name? That is the type of information returned from the function. When you do not have any information to return, you use the keyword void. We’ll talk more about returning later in this chapter. You also used, or called, your new function in main. When you called congratulateStudent, you passed it values. Values passed to a function are known as arguments. The argument’s value is then assigned to