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))
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 takes the inputs, acts on them and works out what the outputs should be.

• And (once you get beyond the more basic programming tasks), there's the bit that decides how to keep the data stored from one running of the program to the next. For an online stock system, how are your products and quantities stored, for example.

It's good practise - from very early in your programming career - to separate out these three areas as much as possible. You'll do so by putting each into its own / own set of named blocks of code, then referencing the named blocks from one another. Not only does this make following the code easier, but it also means you can maintain and debug it block by block, without fear that altering the algorithms will upset the user interface ... and later on you can use the same algorithms with two different user interfaces should you wish, simply by having your named block of code in their own file which you link / build into two or more main programs.

On last week's C Course, I introduced functions - C's word for named blocks of code - early. My very first practical program calculated the Body Mass Index of someone based on their height and weight, and I rapidly moved on to separate out the calculation into a function of its own.

  float getbmi(float w, float h) {
    float bmi;
    bmi = w / (h * h);
    return bmi;
  }


And that's called from the main program as follows:

  bmi = getbmi(weight,height);

The values in the variables "height" and "weight" are passed into the getbmi function, where they're stored for use within that function in variables "w" and "h". They're what's known as position dependent parameters - in other words, the first value passed within the call goes into the first variable named in the definition of the function, the second to the second (and so on if there were more than two parameters).

Once the claculations in the getbmi function have been completed, there's a value (result) to be passed back. This is stated in the return statement which literally means "go back whence you came, taking this value with you". And the value that's in the "bmi" variable within the function gets stored in the "bmi" variable in the main code. It happens to be the same name because I've used the variable name "bmi" in two different scopes, but I could have chosen a different name if I had wanted to do so.

The full source code is [here].

It's good practise, early on, to split your code into named blocks like this. And

• If you find yourself DUPLICATING code, STOP!!! You should be using a named block. Not only will you end up with shorter code, but you'll only have one set of lines to debug / fix / maintain later on, rather than multiple sets.

• If your DUPLICATE code but then CHANGE some sections in one of the copies ... that's not a problem. You have identified the bits that needed to be changed as parameters to the function.
(written 2011-04-09)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller
  [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
  [2199] Improving the structure of your early PHP programs - (2009-05-25)
  [2612] The Model, View, Controller architecture (MVC) - what, why and how. - (2010-02-01)
  [3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
  [3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
  [3705] Django Training Courses - UK - (2012-04-23)
  [3919] What is a web framework? - (2012-11-10)
  [4010] Really Simple Rails - (2013-02-17)
  [4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
  [4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
  [4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
  [4391] Refactoring Perl applications to give them a rosy future - (2015-01-11)
  [4527] Hello Flask world / Python web micro framework - (2015-10-11)
  [4641] Using an MVC structure - even without a formal framework - (2016-02-07)
  [4691] Real life PHP application using our course training MVC example - (2016-06-05)

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)
  [2570] Function Prototypes in C - (2010-01-11)
  [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)
  [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)


Back to
C - a first program that does something useful for you
Previous and next
or
Horse's mouth home
Forward to
Bradshaw, Ben and Bill. And some C and C++ pointers and references too.
Some other Articles
Spring in the countryside near Melksham
Melksham Town Council - vacancy in the Spa Ward
TrainWest, 2011 in pictures - Christie Miller, Bowerhill, Melksham
Bradshaw, Ben and Bill. And some C and C++ pointers and references too.
Using functions to keep look and feel apart from calculations - simple C example
C - a first program that does something useful for you
How we make our programming courses both time and cost effective
Your program - you just provide the filling in the sandwich
C / C++ Course Lunch - sitting out at the West End
Around and about Melksham in more pictures
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/3237_Usi ... ample.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb