Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Function Prototypes in C

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)


Back to
How to run a successful online poll / petition / survey / consultation
Previous and next
or
Horse's mouth home
Forward to
Reading and writing files in C
Some other Articles
Summary of Wiltshire Core Strategy responses
C Structs - what, how and why
The what and why of C pointers
Reading and writing files in C
Function Prototypes in C
How to run a successful online poll / petition / survey / consultation
Forums for your Melksham and open source discussions
Extra MySQL course dates (2 day course, UK)
Excellent staff make for excellent hotel
Bright day, snowy day
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2570_Fun ... -in-C.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb