15)

Program 14 - Functions

Program 14 Functions Source code - prog14.c
This small and simple program shows how to implement functions in C. Functions are like seperate sub-programs that can be executed from the main body of code. If you've programmed in BASIC before, then you will probably know functions as subroutines. Functions are not new, they've been used all along, without realising it: printf, scanf, getch, and rand are all functions with there code written by the programmer of the compiler !

The sample program below has two functions which are called by the main program once, which shows how they can be created and used. If you understood the last program, then this one should be a bit of light relief.

 

Program code- new parts shown in red

#include <stdio.h>

void printname(void)
{
printf("Jason Spooner");
}

void printaddress(void)
{
printf("The Manse,\n");
printf("Ulsta,\n");
printf("West Yell,\n");
printf("Shetland Islands.\n");
printf("ZE2 9BG");
}

void main(void)
{
printname();
printf("\n\n");
printaddress();
}

 

Description of the program code
With this program there is more lines of code 'above' the void main(void) line, which is different to previous programs. This doesn't make any difference though to where the program starts, it still starts at the void main(void) line. The first line it meets up with at the start is printname(); which "calls" the printname function. Near the top of the program code there is a line which reads void printname(void) , this is where the printname function starts. The code to the function is enclosed as a block ( between curly braces ). There is just one line of code in the printname function printf("Jason Spooner"); The red arrows below show the flow of the program, to help understand what happens.

As can be seen from the flow above, the printname function is called, and all the statements inside the block are executed, when the printname function ends it returns to the line below where it was called and execution continues on from there, in this case the line printf("\n\n"); is executed. The printaddress function is then executed and the program terminates.

You can have as many functions as you want in a program, and can give them almost any name you like ( there are only a few exceptions - search for reserved words in the Miracle C language help, for a list ). Function names should be meaningful to help in understanding the code, and they cannot contain spaces. As a general rule, and to be safe, create function names with letters and numbers. The program above is meant as a simplified example to show how to create functions, what they look like and how to call them.

Notice how all along a function has been used, without realising it, the main function. This is a special type of function though that signifies the start of the program. Every program that is created has to have a main function for this reason.

It may seem strange that the main function is placed at the bottom of the program code, but there is a reason for this. A C compiler will compile a program from top to bottom. When it is compiling down, it will meet up with the printname and printaddress functions and effectively make a note of there existence. By the time the compiler meets up with the lines that call the functions it will know about them and know how to handle the calls. If you write the program so that the two functions ( printname and printaddress ) are below the calling function ( main ) the program will not compile.

There is a way around this though if you prefer to have your functions listed at the below the calling function, by using function prototypes. The program below shows these ( function prototypes shown in red ).

#include <stdio.h>

void printname(void);
void printaddress(void);

void main(void)
{
printname();
printf("\n\n");
printaddress();
}

void printname(void)
{
printf("Jason Spooner");
}

void printaddress(void)
{
printf("The Manse,\n");
printf("Ulsta,\n");
printf("West Yell,\n");
printf("Shetland Islands.\n");
printf("ZE2 9BG");
}

This time the program will compile and work fine because the C compiler will meet up with the prototypes for the two functions at the top of the program. Notice the semi-colon on the end of the prototype lines, this is how the compiler knows that these are function prototypes, and not functions. They basically tell the compiler that if it meets up with a call to a function of these names, before the functions themselves, then it's okay they will be following along soon.

With this information header files can now be explained in a bit more detail. With all the previous programs lines have been added at the start of the programs such as #include <stdio.h>. In program one, the guide was to just include the line and don't worry too much about what it did at that time. Header files generally contain a list of function prototypes for compiler functions like printf, scanf etc.. The code for the functions themselves cannot be seen, ( unless you register Miracle-C and get the source code ) but the code in the header files ( containing the prototypes can be seen ). They're in the INCLUDE folder of Miracle-C.

So in short the line #include <stdio.h> is saying include a load of prototypes for some other C routines, into the program.

 

Summary
The code and description above showed how programs can be divided into small manageable chunks by using functions. They are used everywhere in programs of any great size. Without functions a program of say 1000 lines + would be difficult to write and manage. This page also showed part of the process the compiler goes through when compiling programs, more importantly the order it does it. As a result you cannot call a function that is lower in the source code than the calling function, unless you have a function prototype defined somewhere above in the code.

The only part now which needs explaining is the void part, which has been used over and over without explanation so far. With this information functions become a lot more useful and useable, as will be seen on the next page..

 

Tasks

There are no tasks for this page.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001