For 2023 - 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)) |

Well House Consultants
You are on the site of
Well House Consultants
who provide
Open Source Training Courses
and business
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
|
C and C based languages module C204
Functions, Macros and programs in multiple files
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 and C++, Programming in C, C and C++ Programming, Learning to program in C and 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 | 4557 | Function prototype - what they are and why you should use them - C and C++ In C and C++, your program if it's anything but trivial will be written in multiple source files. Each will be compiled into a snippet of runnable code - an "object" file - before all of these object files are linked / loaded together, and with standard libraries, using a program known by various names ... | 2015-10-27 | 4555 | Preprocessor directives in C and C++ - what they mean The C pre-processor runs on your C and C++ code at compile time prior to the main compile process. Source lines starting with the # character are processed to roduce a new source code file (internally - you don't normally see this) which is then the input to the compile process "proper".
Preprocessor ... | 2015-10-27 | 4554 | Passing information into functions in C - by name, by value If you pass a variable into a function in C, you copy the contents into the function so thst any changes made internally in the function are not reflected in the main code copy. However, if you pass in the address of the variable and work at the same address inside and outside the function, any changes ... | 2015-10-27 | 4338 | Passing arrays into functions in C When passing arrays into functions in C, you can specify just the array name and C will pass in a pointer to the first element of the array (i.e. &abc[0] is the same as saying just abc ). Within your functions, you can then use array style notation with square brackets, or pointers.
There's an ... | 2014-12-02 | 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 ... | 2012-05-12 (longer) | 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.
If ... | 2012-05-05 | 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
abcpointers.c | Ways of passing back results from a function | astamp.c | changing a parameter's value within 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 |
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 [here].
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
Ruby,
Lua,
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.
|
|
|