More of Functions in C++

There are a few things that aren’t really covered in the Overland text related to functions – at least not in chapter 5 – that are very important to understand.

As a note, I very (very very very) frequently use different terminology for the variables that are part of the function header vs. function call.  Here’s my explanation of the two:

  • function parameters:  the “things” listed in the function header or function prototype.  They accept values from the function call. You may see these referred to as “formal parameters”.
  • function argument:  these are the values that are listed in a function call, sometimes literal values, sometimes variables. You may occasionally see these referred to as “actual parameters”.

What Happens When you Call a Function

So, functions are important in C++.  So, since they are so fundamental to the language (and many other languages for that matter), it is always helpful to have a better understanding of what happens behind the scenes when you call a function.  Rather than trying to explain it in words, I think a video would work best because pictures help.  Here is an excellent YouTube video that provides a great explanation:  Call Stack, Scope & Lifetime of Variables.  He also includes info on the notion of scope and lifetime of variables, which you may hear me refer to from time to time.

Types of Parameter Passing

In Java, when you pass a variable to a function (regardless if that variable is primitive type or an object reference), the value stored in that variable is copied into the parameter of the function.  This is called Pass by Value.  There’s a lot of misinformation out there (where ever there is) about this, and “they” will try to convince you that Java has a second parameter passing method called pass by reference.  It doesn’t; it is probably just a big conspiracy.  I digress…

However, c++ does implement honest-to-goodness pass by reference.  When we talk about pass by value or pass by reference, we’re talking about what fundamentally gets passed to the function.  Here’s the nitty-gritty:

  • Pass by Value: a copy of the argument is copied into the parameter (sometimes called a value parameter).  Modifications to the parameter will in no way change the argument.  It is OK to pass a literal as an argument to a value parameter.  Of course, it is fine to pass a variable as an argument to a value parameter as well.
    • //a function header for pass by value
      //looks pretty much like you'd expect it to look
      void power(int x, int y);
  • Pass by Reference:  the memory address of the argument is passed to the function parameter (sometimes called a reference parameter).  So, modifications to the parameter will modify the argument as well.  In essence, the reference parameter is like an “alias” for the argument that was passed to it.  As you might imagine, it does NOT work if you try to pass a literal value as an argument to a reference parameter.  So how do you indicate pass by value vs. pass by reference in code?  Easy peasy: Put an ampersand (&) between the datatype and the parameter name in the function header.  In a function prototype, you can of course leave off the parameter name, but you still need the ampersand.
    • //notice the & between the data type and name of the parameter
      void someFunFunction(int& a, int& b);

You may now start dreaming about the words ‘value’, ‘reference’, and ‘parameter’ given the number of times they’re used in the preceding points.  However, they are so important to c++ that dreaming about them can only be a good thing… and understanding them will help you immensely as you progress in programming.

Here are a couple Youtube videos that give some decent examples of Pass by Reference and Pass By Value.  Video 1Video 2, Video 3 (related to vid2 and vid3, note that he’s using MS Visual Studio, so some of the code included is for that environment such as system(“Pause”)).

Speak Your Mind



This site uses Akismet to reduce spam. Learn how your comment data is processed.