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)
Some other Articles
Spring in the countryside near MelkshamMelksham Town Council - vacancy in the Spa WardTrainWest, 2011 in pictures - Christie Miller, Bowerhill, MelkshamBradshaw, Ben and Bill. And some C and C++ pointers and references too.Using functions to keep look and feel apart from calculations - simple C exampleC - a first program that does something useful for youHow we make our programming courses both time and cost effectiveYour program - you just provide the filling in the sandwichC / C++ Course Lunch - sitting out at the West EndAround and about Melksham in more pictures