Bradshaw, Ben and Bill. And some C and C++ pointers and references too.

I have a scarse original of the January, 1960 edition of Bradshaw's guide. Bill and Ben are on my course, and they both want to take a look at it. So I take a perfect photocopy of it, and I pass that copy to Bill with the original handed, with great reverance, to Ben.
Bill and Ben both spend some time studying table 138, and when I'm not looking they add some notes for future reader, Bill's in blue ink and Ben's in Green. Both give me a curteous "Thank you" when they're done.
Barabara comes on a course a couple of weeks later, and - talking about rail services as they were in 1960 to Hirwaun, I hand her the old Bradshaw's. "What a pity about these notes in green ink" she says and, indeed, when I look at the old guide I see that it's been defaced in green. And there is no blue ink to be seen.
There's a parallel to this in programming:
• When you call a function, you can "call by value" which means you pass in a copy of the data. That's what we did when we handed the copy to Bill; Bill can read, study and make use of the copy that he's been given but any changes that he makes are to his (second / private) copy, and although he can say "thank you" and tell you his deductions at the end, he's not able to alter the original.
• You can also "call by name" / also known as "call by address". In this case, you're not duplicating the data - you're just telling the function where abouts in the computer memory it's to find the original so that it can access it directly. Much more efficient, but more flexible, but also much more trusting (by the calling code) of the function. Any alterations made by the function are reflected in the original.
In C and C++, You can use both "call by value" and "call by address". Here's a call to a fucntion which passes in a value:
lots = triple(stuff);
and here's one that passes in an address:
oodles = quadrle(&thing);
With the & meaning "memory addresss of"
Within the function, I have to declare my data type to be a regular variable in the first case, or a pointer in the second case:
int triple(int incoming) {
and
int quadrle(int * incoming) {
And then I have to use "*" to mean "Contents of" as I reference the variable in the pointer / call by address version. So in the first function I write
outgoing = incoming * 3;
incoming = incoming - 4;
but in the second function I write
outgoing = *incoming * 4;
*incoming = *incoming - 5;
with the first example NOT altering the value of "stuff" in the calling code, but the second example ALTERING the value of "thing" in the calling code.
C++ also lets you declare
references - which are lightweight aliases to variables and gibe you much of the power or pointer without the need to use the extra * as you access the variable's content. And it's critically important that you understand the whole business of pointers and reference if you're using C and C++. I find that parallels, comparisons such as the one I started with help delegates get to first base on the subject on our
C and C++ courses, and we introduce the subject early on so that everyone gets plenty of chance to get plenty of exposure to and practise with them ... getting to 2nd and 3rd base during the rest of their course. There's a C++example showing references
[here].
The complete source code used for the example lines above is
[here] ... and I've also added some sample output so you can see how it works.
Want to learn nore? Come on our C or C++ course - we've got different course to suit you no matter what your starting point!
[link to choose] (written 2011-04-09, updated 2011-04-17)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C234 - C and C based languages - Further C++ Object Oriented features [801] Simple polymorphism example - C++ - (2006-07-14)
[802] undefined reference to typeinfo - C++ error message - (2006-07-15)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[1159] It can take more that one plus one to get two. - (2007-04-22)
[1819] Calling base class constructors - (2008-10-03)
[2004] Variable Scope in C++ - (2009-01-22)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2576] What does const mean? C and C++ - (2010-01-15)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2849] What are C++ references? Why use them? - (2010-07-02)
[3057] Lots of things to do with and within a C++ class - (2010-11-16)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3982] Using a vector within an object - C++ - (2013-01-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[4559] When do I use the this keyword in C++? - (2015-10-29)
C207 - C and C based languages - Pointers and references [1155] Pointers in C - (2007-04-19)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[1497] Training Season Starts again! - (2008-01-07)
[2572] The what and why of C pointers - (2010-01-13)
[2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[3121] New year, new C Course - (2011-01-05)
[3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
[3386] Adding the pieces together to make a complete language - C - (2011-08-11)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[4128] Allocating memory dynamically in a static language like C - (2013-06-30)
[4560] Variables, Pointers and References - C and C++ - (2015-10-29)
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 End