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
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|
|3721||Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks|
Soon after you start to program, you'll learn that you want to re-use code. And that re-use will sometimes come in the form of loops, and at other times in the form of named blocks of code which you'll call up from multiple places in your program, or indeed from multiple programmers.
• A good ...
|3717||Returning extra results from a function in C|
There are three ways of returning a result from a function in C. You can use return to pass a result back, you can use a variable declared outside all of your function (so it's global and accessible everywhere), and you can pass in a pointer to act at the target for where a result is to be saved.
|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 ...
|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
|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 ...
|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 ...
|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 ...
|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 ...
|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 ...
Examples from our training material
|abcpointers.c|| Ways of passing back results from a function|
|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|
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
Parameters and return values.
Variable scope and global variables.
Header files and other Macros
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
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.