Exercises, examples and other material relating to training module C204. This topic is presented on public courses
Learning to Program in C,
Learning to program in C++,
Programming in C,
C and C++ Programming
Background
As your code grows from 5 to 50 to 500 lines, it becomes
less managable as a single block. Functions allow code to be
split into more managable blocks, which can then be saved into
separate files if you like so that the same logic can be shared
between a number of different programs where appropriate.
| Articles and tips on this subject | updated |
| 3237 | Using functions to keep look and feel apart from calculations - simple C example There are a number of distinct elements in any program.
• There's the look and feel of the program to the outside world - what it says as it prompts, how its forms are displayed on a web page, the formatting of the results, how it reports errors, etc.
• There's the calculation bit that ... | 2011-04-09 |
| 2841 | C Course exercise and sample answer - source in 2 files Here's a sample exercise from the C course ... set yesterday to tailor the course for the particular group of delegates that we have this week:
/* Complete my program ...
... in 2 files
... fixed VAT rate at compile time
Function params - cost per item (net) and
no. of items at this cost
tc ... | 2010-06-30 |
| 2575 | Sharing variables between files of code in C - extern In C, If you want to share a variable between a number of functions, you can declare it at the top of your file, outside the functions. It's then an element that's held in memory all the time and available to all your functions. Since your functions are separate elements which are joined together at ... | 2010-01-15 |
| 2570 | 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 ... | 2010-01-13 |
| 1478 | Some new C programming examples - files, structs, unions etc A series of new "C" examples have been added to the web site - a course for a small group allowed me to go somewhat away from the more normal run yesterday and write some new illustrations in front of them.
Call by value v call by pointer comparison: link
Using extern to share a variable between two ... | 2007-12-19 |
| 1163 | A better alternative to cutting and pasting code If you're new to coding, you'll be so concerned to be writing code that works that you may not take a look at coding technique. Your nose will be so close to the grindstone as you work that you won't take the time to look and ask "Do I need to keep grinding anyway?"
If you find yourself writing a piece ... | 2007-04-26 |
| 775 | Do not duplicate your code If you've writing or maintaining a program and you find yourself cutting and pasting a chunk of code, STOP and think again.
By duplicating a block of code, you're duplicating your maintainance task from that point onwards - any fixes applied to the original much be applied to the copy too. And that's ... | 2006-06-30 |
Examples from our training material
| fundy.c | call by value v call by pointer / reference |
| funfiles.c | The home for an extern variable |
| funfuncs.c | use of extern to reference variable held elsewhere |
| healthcheck.c | function prototypes and extern |
| healthybits.c | functions to be loaded from another program |
| lp3.c | Variable declaration and scopr in inner blocks |
| makefile | makefile for module C204 |
| p3.c | Returning values from a function |
| taxi.c | functions for VAT rate exercise |
| tcall.c | Function definition and call |
| tcmain.c | Using functions |
| tcsub.c | Functions in their own file |
| tcsub.h | Using functions - the header file |
| third.c | Separating look and feel from business logic |
| twice.c | Function names must be unique in C |
| twice.cpp | Two functions of same name in C++ |
| xrcz.c | Sample answer - main code for VAT exercise |
Background information
Some modules are
available for download as a sample of our material or under an
Open Training Notes License for free download from
http://www.training-notes.co.uk.
Topics covered in this module
Defining functions.
Parameters and return values.
Exercise.
Function prototypes.
Variable scope and global variables.
Constants.
Header files and other Macros
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our
Listing and schedule page.
Well House Consultants specialise in training courses in
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if
you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre. Also
available is the Opentalk
Forum for discussion of technical questions.