1	#include <stdio.h>
2	#include <stdlib.h>
3	
4	/* This function has no parameters and returns nothing */
5	void DisplayMenu()
6	{
7	  printf("\n1. Calculate 5 + 4\n");
8	  printf("2. Print 3 Names\n");
9	  printf("3. Quit this lunacy\n");
10	}
11	
12	/*This function has two parameters both are integers it adds
13	these together and returns a integer which is the result*/
14	int Calculate(int FirstNumber, int SecondNumber)
15	{
16	  return FirstNumber + SecondNumber;
17	}
18	
19	/*This function has no parameters and returns nothing*/
20	void DisplayNames()
21	{
22	  printf("\nThree Names\nHumphrey\nIngrid\nPeter\n");
23	}
24	
25	int main(int argc, char *argv[])
26	{
27	  int UsersChoice = 0;
28	  
29	  while (UsersChoice != 3)
30	  {
31	    /* Call function to show menu */
32	    DisplayMenu();
33	    scanf("%d",&UsersChoice);
34	    if(UsersChoice == 1)
35	        printf("\n5 + 4 = %d\n",Calculate(5,4));
36	    else if (UsersChoice == 2)
37	        DisplayNames();
38	  }
39	        	
40	  return 0;
41	}

Purpose of the program

The purpose of this program is to demonstrate the use of functions. There are three functions used. The first displays a menu. A function allows you to reuse the same code many times. Therefore it is perfect for a menu which you want to display many times. The second function adds two numbers together and returns the sum. The third function displays three names related to Casablanca. Of course you could say that there is another function, that is the main function.

Break down of the code

Line 1 - This line starts with a '#' hash sign which indicates that it is a preprocessor directive. The #include directive instructs the preprocessor to copy the contents of the following file into this source code file at this point. The filename is surrounded with '<>' brackets this instructs the preprocessor to look in the system 'include' directory first for this file. So this line copies the contents of the file 'stdio.h' into the source code file at this point before compiling starts. The file 'stdio.h' contains the declaration of many basic input output functions that are used in C programs.

Line 2 - This line works in the same manner as line 1. The difference is that the preprocessor copies the contents of the file 'stdlib.h' in to the source code at this point before compiling starts. The file 'stdlib.h' contains the declaration of many basic functions used by C programs.

Line 3 - Is blank.

Line 25 - This line contains the start of our definition of the main function. As you can see main returns an int value. This value indicates to the operating system whether or not the program has terminated normally. The main function also takes two parameters these contain any command line options that are sent to you program when it starts. E.g. if you start notepad from the command line you would write notepad if you want to start notepad with a file to edit you would write notepad readme.txt. readme.txt would be a command line option. This is not a subject that will be considered further in the book so you may wish to find out more yourself.

Line 26 - This line contains the opening brace of the main function. There must be a closing brace to match every opening brace and vice versa. The closing brace is on line 13.

Line 40 - The return statement is used at the end of functions to return an item of data. This item of data should match the return type declared at the start of the function. We are in the main function and as you can see in line 6 this should be an integer. The return statement marks the end of the function any code after this will not be executed your compiler will usually warn you of this when you compile the program. The value 0 is a value that tells the operating system that the program has terminated normally.

Line 41 - This line contains the closing brace of the main function.