An Introduction To Functions
Welcome to Colour Me Cocoa.
Looks like you're new here. If you like what you see, you may want to subscribe to my RSS feed, or if you prefer sign up to receive email updates.
Thanks for visiting!
Last Time we discussed controlling the flow of your program. This allowed us to change what it was our program did based on the value of j. Previous to that we discussed using loops to make short work of repetitive processes. It’s now time to learn about functions, another way of taking repetitive work and making it easier.
Defining A Function
You’ve already seen a function, it’s the main function. Writing your own function is simply a matter of copying the syntax of the main function:
RETURNTYPE FUNCTION(TYPE PARAMETER) {
return RETURNTYPE;
}
The RETURNTYPE section identifies a type returned by the function. The main function returns an int. You can return any type you want and this will be useful later on when working with non-trivial problems, but for now we will return an int. In defining an int as the return type we must also make sure that the return statement at the end of the function correctly returns an int.
int FUNCTION(TYPE PARAMETER) {
return 0;
}
Next we need to decide what our function is going to do. Since we already have a series of loops being called in the MyFirst program I’ll be creating a function for each of those loops. Therefor I present to you the forLoops function!
int forLoops(int repeat) {
int i;
for(i = 0; i < repeat; i++) {
printf("This is a for loop within the forLoops function\n");
}
return 0;
}
So lets take a look at what has changed. Firstly FUNCTION has been replaced with something that makes more sense, forLoops. It’s always useful to have function names that actually mean something.
Secondly TYPE PARAMETER has been replaced with int repeat. This is the first (and only) parameter of the forLoops function. You can add as many as required by using a coma to separate them (hint: Look at the main function). I’ve named this parameter repeat because it is used to instruct the forLoops function on how many times it should repeat the for loop.
Third and finally I’ve added a for loop that prints out the string “This is a for loop within the forLoops function” until i reaches the number represented by the repeat parameter.
Using A Function
So how can you put your shiny new function to good use? Simple. First you need to include the function in your source code. Add it underneath the closing brace of the main function. Secondly you need to call your function. Add forLoops(5); to your main function. You should end up with the following source code in main.c:
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
forLoops(5);
return 0;
}
int forLoops(int repeat) {
int i = 0;
for(i = 0; i < repeat; i++) {
printf("This is a for loop within the forLoops function\n");
}
return 0;
}
Moving Forwards
I’ve given you the code for the forLoops function. How about you create yourself a whileLoops and doWhileLoops function? If you’re feeling extra-adventurous then take then create another function to control the flow of your 3 looping functions.
Next time we’ll take a look at how to split your code up into multiple files and why it’s often useful to do so.