Programs other than the shortest are name up of named blocks of code (functions) into which parameters are passed and from which results are returned. And rather than have all the functions that make up an application in one file of source, you'll share them between several. That way, maintainable is in more manageable chunks, and you can use parts of your code in several programs by loading the same named blocks into a series of programs that share them.
But there's a bit of an "issue" with that, in that at the time you compile each of the individual files of functions (prior to loading them *all* together), you're in danger of calling a function with the wrong number and type of parameters, and to not be "picked up" on the error by the compiler either. If you've even seen
"Segmentation fault - memory violation" messages from a program, there's a good chance that they were caused by an error of this type that wasn't checked. Kernighan and Ritchie, two of the originators of C, wrote the definitive text book on the language and this version of the language is known as "K&R C".
Under more recent C - "ANSI C" - there's a solution. At the top of each file of your C source, you should include a
function prototype - a template, if you like - for each function that you'll call, so that the compiler CAN make its checks, and segmentation faults / segmentation violations can be largely pre-empted.
Here are some sample function prototypes:
float gimme_table(float, int );
void silly_message() ;
The gimme_table function takes two parameters - a float and an int, and returns a float. silly_message may or may not take parameters (use the word
void in the brackets to say it never takes any parameters) and returns nothing.
So far so good ... but do you REALLY want to repeat all the function prototypes in every file of source - if you do, you have just created a maintenance problem. So you group them all together into a file which you load through a
#include directive to your C preprocessor.
By default the ANSI C compiler will complain strongly about missing function prototypes ... so that you've now got a scheme in which you maintain a single file that lists all your functions, and which will tell you straight away at compile time if you've accidentally created some code which doesn't match what you have set up, rather than having to find out about it when debugging some obscure behavior much later on.
There's a complete example - all three files - on our web site; a new example written earlier today on the
C Programming course
1.
The main program
2.
The functions that it uses
3.
The file of function prototypes that's called in by both of the other two.
(written 2010-01-11, updated 2010-01-13)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C204 - C and C based languages - Functions, Macros and programs in multiple files [775] Do not duplicate your code - (2006-06-23)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[2575] Sharing variables between files of code in C - extern - (2010-01-14)
[2841] C Course exercise and sample answer - source in 2 files - (2010-06-30)
[3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
[3717] Returning extra results from a function in C - (2012-05-03)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[4338] Passing arrays into functions in C - (2014-12-02)
[4554] Passing information into functions in C - by name, by value - (2015-10-26)
[4555] Preprocessor directives in C and C++ - what they mean - (2015-10-27)
[4557] Function prototype - what they are and why you should use them - C and C++ - (2015-10-27)
C203 - C and C based languages - Conditionals and Loops [353] Wimbledon Neck - (2005-06-20)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[3121] New year, new C Course - (2011-01-05)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3243] Breaking the running sequence - an introduction to conditional statements and loops - (2011-04-11)
[3384] Loops - a comparison of goto, while and for - (2011-08-10)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
Some other Articles
Summary of Wiltshire Core Strategy responsesC Structs - what, how and whyThe what and why of C pointersReading and writing files in CFunction Prototypes in CHow to run a successful online poll / petition / survey / consultationForums for your Melksham and open source discussionsExtra MySQL course dates (2 day course, UK)Excellent staff make for excellent hotelBright day, snowy day