Home Classroom C++ C++ with Barney! Class 7

C++ with Barney! Class 7


More on Functions

[You're Ted and you decide to learn some C++ from your friend Barney. This is the seventh day of the class]

You: Hi, Barney!

Barney: Hi, Ted! How are you?

You: Fine as always! And you?

Barney: Never better! So what were we doing last time?

You: Mm, we wrote some functions and you said we will be discussing about global variables this time.

Barney: Ah, yes, we did write two programs using both global and local variables, didn’t we?

You: Yeah, and the one with global variables seemed easier.

Barney: Well, Ted, it may seem easy at first, but when you go deeply into programming global variables can be a real mess. And do you know why? Global variables have the vulnerability of being possibly changed by a function or code anywhere in the programme, so when something goes wrong while debugging it becomes a hell of a work to find out where the bug comes from, because it may have arisen from anywhere.

You: And with local variables?

Barney: And with local variables, since they are only limited in scope to the function they’re declared in, debugging would be limited to that function. Usually we don’t want our variables to be interfered by outside forces.

You: But how can we possibly mess up with the variables when we’re quite aware that we’ll be messing up if we changed the values in a bad manner?

Barney: That’s only when you’re writing the whole programme, buddy. Possibly different functions may be written by different people and when each other does not exactly know the behavior of these variables it can become a real mess.

You: Ah, I get it.

Barney: Okay, so let’s see about assigning default values to the parameters of a function. These default values are used when the person who calls the function does not send enough parameters to the function. Today we will be using the VI editor in Linux, instead of Visual Studio we used in Windows. VI is one of the coolest text editors you can use in Linux.

You: Aren’t there IDEs like Visual Studio for linux?

Barney: Of course there are, like Code::Blocks, Anjuta, NetBeans and Eclipse and even other sophisticated text editors like Emacs. But I personally prefer VI.

You: Okay

Barney: Look at this programme:

See how the value of b is set to 1 in the function? So in the main function, when the user does not pass a second value to the function, it automatically assigns 1 to b.

You: So the answer to multiply(3) will be 3 itself?

Barney: Exactly. Let’s run and see:


You: Yeah.

Barney: Okay. In this programme we could only multiply integers. What if we also want to multiply doubles as well?

You: Mmm, may be we’ll have to write another function for that.

Barney: Yes, but there’s a way to avoid writing duplicate functions for this by using a type called templates. But this is a rather advanced topic so we’ll deal with that later on. However, there’s another interesting technique called operator overloading which lets us write several functions with the same name and parameters but different data types for those parameters. Let’s write one first and you’ll understand this. Let us expand our previous programme like this:

You: So how does the compiler know what function to call when multiply is called?

Barney: The compiler looks at the data types of the parameters passed. If they’re integers it will call the first function and if they’re doubles the second one will be called. What will happen if the parameters belong to none of these types?

You: Compile error?

Barney: Yes, an error will be given because there are no functions with a signature similar to the given syntax. Let’s see the output of this programme:

That wasn’t that difficult, wasn’t it? Hope you don’t need any more explanation on operator overloading.

You: Yeah, it’s crystal clear.

Barney: Good. Now that you know how to handle basic functions, let’s write a programme that actually uses them.
This will be a daunting one at first, but if you observe the functions separately it’s very simple.

You can see how all the big work is broken into small functions and the main function merely controls them.

You: Wow, it seems writing functions can really simplify the things!

Barney: Yeah, try to write some by your own. So let’s meet again next time!

You: Okay, thanks a lot! Bye, Barney!

Barney: Bye, Ted!



Engineer. Loves tic-tac.


Leave a Reply