Guides:C/C Crash Course/Functions

From CoderGuide

Jump to: navigation, search

Back to TOC


Every time you program, there is some code that you will want to run over again, or reuse in another program. This allows you to extend the usefulness of the language. In order to do this, we can write our own functions, and call them rather than having to retype our code over and over again. Using functions also makes for cleaner code. As a general rule of thumb: If you run any code more than once, or think you may want reuse that piece of code in another program, then you should write a function for that code. Functions should be dedicated to a single task, not many, largely unrelated tasks such as:"load database and display list on screen." Function names should also reflect what they do.

C comes with many functions for your use in what is called the standard C library. These functions perform many common, and not so common, tasks that allow you to write useful programs much faster than it would if you had to write them yourself. Also, because this is a standard library, you can safely assume that these functions will exist on all systems that have a standard C compiler. This means that your code will compile on all systems, whether it be a PC running Windows or Unix, or a Mac, or even a super computer. Some of these functions you've already seen, such as printf(). We will be covering many more in the sections to come, and in the appendix. We will only be covering the a subset of the functions in the ANSI/ISO C library.

That said, lets write and use our own functions! In fact, you already have written one function on your own: main(). The main() function is a very special one in that it is the first function that is called when your program is run. Basically, it is the starting point of program execution. You can, if you really want to, call the main() function again, but that's probably not something you want to do, and can cause an infinite loop if you're not careful.

To define a function, you need to decide on a function name, using the same naming rules for variable label names (see Variables section). ANSI and ISO C standards require that you also give a function prototype for all functions, besides main(). When you use a header file, such as stdio.h, it includes many definitions, including function prototypes so that you may use functions in that library (in this case, the standard C library). A function prototype basically tells the C compiler how it is supposed to call a function, and what type of parameters it accepts.

In addition to parameters, a function can have a return value. The data type of the return value is given to the right of the function name, much like you would define a variable. This allows your function to return some value based upon whatever process it did to the data inside. If a function will not return a value, the we specify the void data type. The main() function always returns an integer. You can, if you like, put void in the parenthesis of the function definition if the function has no parameters, but you don't have to (Java doesn't allow this).

When writing a function prototype, you only need to give the data types that the function takes as parameters, not the label names (but you can if you really want to). Also, the function prototypes should appear towards the beginning of the file. Here is an example to make things a little more clear:

Writing our first functions!
/* Load some function prototypes in the standard C library, including
the one for printf()*/
#include <stdio.h>
 int square(int a);
 Squares the number a, and returns the result.
int square(int a); /* we could have also written int square(int);*/
 int power(int x, unsigned y); 
 Returns x raised to the y power
int power(int,unsigned);
int main(){
        int num=5,i;
        i=square(num); /*square the number num*/
        printf("5 squared is %d\n",i);
        printf("2 to the 8th power is %d\n",power(2,8));
        printf("2 to the 0th power is %d\n",power(2,0));
        printf("2 to the 2th power is %d\n",power(2,2));
}/*end function main()*/
/****** square() ******/
int square(int a){
        return a*a; /*returns a*a */
}/*end square()*/
/***** power() *****/
/*Because we didn't give label names in the function prototype above,
we can use whatever labels we like to here. */
int power(int num, unsigned power){
        int i=power; /*i will be our counter*/
        int ret=1; /*ret will keep the result*/
        if(power==0) return 1;
                ret*=num; /*multiply ret by a, and store result in a*/
        return ret;
}/*end power()*/

Okay, the boring part is over, now time for the fun stuff!

Personal tools