Training, Open Source Programming Languages

This is page

Our email: • Phone: 01225 708225

Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
Loops and functions
Introduction to C example from a Well House Consultants training course
More on Introduction to C [link]

This example is described in the following article(s):
   • Learning to write good programs in C and C++ - separating out repeated code - [link]

Source code: second.c Module: C101
#include <stdio.h>

/* Functions and Loops - in this example all in the one source file

float ohgawd(float separation,float linkcount) {
        float crossings;
        float ends;
        float returns;

        crossings = separation * linkcount;
        ends = 2.0 * 1.76 ; /* to tie off in a knot */
        returns = (linkcount -1.0) * 1.32; /* return around post */

        return crossings + ends + returns;

main() {
        printf ("How long a piece of string?\n");

        float gap;
        float mn;

        float nstr;

        nstr = 1;
        gap = 6.5;

        printf ("Gap is %.2f\n",gap);

        while (nstr < 10.0) {

                /* Hide calculation is separate function which can be
                looked after by the expert who knows all about this stuff.
                At a later point, the function can go in a separate file
                and (1) be used in lots of programs and (2) carefully tested
                on its own! */

                /* the name used here IS the same name that will be used to
                define the function. BUT variable names are not shared. That
                makes sense - you don't want to call in Old Billy's code and
                find you can't use "Boris" and "Ken" as variable names as he
                has already done so! */

           mn = ohgawd(gap,nstr);

                /* P.S. Passing everything over a single interface also means
                you can be certain of what the function needs and not have any
                nasty surprises about things it gets from elsewhere! */

           printf ("If you want %4.0f strings, ",nstr);
           printf ("you need %6.2f metres\n",mn);
           nstr = nstr + 1.0;


/* Ouput from this program:

munchkin:c grahamellis$ ./second
How long a piece of string?
Gap is 6.50
If you want 1 strings, you need 10.02 metres
If you want 2 strings, you need 17.84 metres
If you want 3 strings, you need 25.66 metres
If you want 4 strings, you need 33.48 metres
If you want 5 strings, you need 41.30 metres
If you want 6 strings, you need 49.12 metres
If you want 7 strings, you need 56.94 metres
If you want 8 strings, you need 64.76 metres
If you want 9 strings, you need 72.58 metres
munchkin:c grahamellis$


Learn about this subject
This module and example are covered on the following public courses:
 * Learning to Program in C
 * Learning to program in C and C++
 * Programming in C
 * C and C++ Programming
 * Learning to program in C and C++
 * C and C++ Programming
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering C and C++ are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Introduction to C" training module. You'll find a description of the topic and some other closely related examples on the "Introduction to C" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Web site author
This web site is written and maintained by Well House Consultants.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

© WELL HOUSE CONSULTANTS LTD., 2020: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho

PAGE: • PAGE BUILT: Sun Jan 31 13:04:01 2016 • BUILD SYSTEM: WomanWithCat