Make it Function: Chapter 5 of Overland

In many ways, functions are the basic unit of work in a c++ program.  Every program that we write will have at least one function – the main function.  From Chapter 5, you can skip (or skim) the following stuff:

  • Section 5.6 – Variable-length args.  These can be cool and useful at times, but don’t show up very much for us.
  • Section 5.7 – Lambdas.  Lambdas are awesome.  But, they can be confusing, so in this shortened class, we’ll probably bypass them.  Read them if you’d like.
  • Section 5.8 – constexpr functions.  Cool, but not immediately important for us.


In Java, the relationship between a function call and where the function is implemented (within a particular class) is nonexistent.   When the c++ compiler is working on a particular c++ file and it encounters a function call, it must have already seen some information that will let it know how to verify if the function call is legal or not.  This info can be gathered from 2 different sources:

  1. The logical place is the function header – the first line of the function that gives the return type, the name, and the parameters of the function.
  2. A function prototype, which effectively says to the compiler, “hey, compiler, here is info for how a function call should look, but I’m not going to define the function until later.”  A prototype is essentially the same as the function header, but it is terminated with a semi colon rather than a function body.   See section 5.1 (5.1.1 and 5.1.2, in particular) in the Overland text for more detail.

It is really important that you be able to quickly discern what someone is meaning when they say function header, function definition, function prototype, and function call.  So, learn what those words/phrases are referring to.  It is important to also know what someone means when they say function declaration vs.  function definition (sometimes called function implementation).

Overloading Functions

Function overloading is pretty cool.  You can have 2 functions with the same name but different parameter lists.  The compiler will determine at compile time which of the two (or more) functions you’ll need based upon the function calls.  Sometimes students struggle with figuring out if two functions are actually overloaded.  A couple points about this:

  • They have to have the same name.
  • No one cares about return types when thinking about function overloading.  Why?  Well – remember that in an assignment statement in c++ (e.g. myFunVariable = someCoolfunction(1, 2, 3);), the right side of the assignment operator (=) is going to be evaluated first before ‘it’ even looks at the left side.  So – there ya go – no return type consideration.
  • Think about the job of the compiler.  When it encounters a function call, it has to determine if it is a legal function call or not.  So, if there are two functions that have the same name and parameter lists that are too similar, it won’t be able to discern which of the functions to call.  Thus the two functions become ambiguous.  Your code will not compile.  See Code Sample 1.

//Code Sample 1

#include <iostream>
using namespace std;

//2 function prototypes.  Think about how the compiler
//would differentiate between these two functions
//for "example call 2" below in main.  "Example call 1" is no
//problem, but #2 would cause confusion.  So the compiler
//would like these two function prototypes being "visible" at the
//same time.
int functionA(int val);
int functionA(int val = 3); 

int main () {
    int myVar = 10;
    functionA(); //example call 1.
    functionA(myVar); //example call 2.
    return 0


Other things to note

  • Section 5.2 speaks of the evil global variable again.  Don’t use them.  (In all seriousness, there are times when it makes sense to have a global variable.  I don’t want to come across as one of those cynical academic types that makes broad statements with no support.  However, for beginning programmers, they can really cause you a lot of headache.  For more info, look here and, for a much more in-depth coverage, here.)
  • Section 5.3 covers detailed function declaration syntax.  Read it.  Don’t worry about the C++11 stuff for now, or or virtual.  We’ll get to virtual functions later in the “semester”.

Speak Your Mind



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