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

C++ with Barney! Class 6



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

You: Hi, Barney!

Barney: Hi, Ted! I was waiting for you.

You: I was a little late. So what shall we be doing today?

Barney: We’ll be discussing about functions today. Take a seat.

You: Thanks!

Barney: You see, Ted, all the programmes we wrote until now were stuffed inside the main method. But when our programmes become complex, this main method may get too long and too difficult to handle. What would you do to prevent your programme getting too long?

You: Mmm, break it into parts?

Barney: Exactly. When we break a programme into parts or “modularize” it, everything becomes easy to handle. One way to modularize our programmes is to break them into functions. There are two kinds of functions, built-in ones and user-defined ones. Built-in functions are provided by C++ libraries, we’ll get to know them as we progress. For now let’s consider about the user-defined ones.

You: Okay.

Barney: Functions are meant to perform some action specific to them. For example, say we have to write a function that would give you the square of a number. What data does the function need to calculate the square?

You: The number, of course.

Barney: Yes, we have to “pass” the number to the function as a “parameter” and the function would “return” us its square. The syntax of a function is as follows:

return_type function_name (parameters) {


You: Hmm, that’s a pretty complex syntax to have.

Barney: Not after now. Let’s write a sample function, one that would return the square of the input parameter.


You: Okay, you’ll have to explain me that from top to bottom.

Barney: It’s like this. First, you mention the return type. Since we’re going to input an integer into the function, the return type would also be an integer. Next comes the function name “square”. You can choose any name you want here, just like in variables, but make sure you choose a descriptive name. What do you call the “int x” part within the brackets?

You: The parameters?

Barney: Yes, in this case there is only one parameter. If there were more, we would have to separate those using commas. So this ends the “header” of the function. Next is the body of the function. And that is where the actual function statements go.

You: Okay, I think I understand what the body part does. It assigns the square of the given value to an integer and returns that value.

Barney: Yes! You’re really smart! This function is only a part of the programme. Let’s write a complete one with this. Functions are written outside the main method, like this:

Let’s run it and see:

You: Hmm, works fine.

Barney: Note that in our function we used:

However we could have achieved the same thing from a single statement, like this:

You: Ah, yes.

Barney: Okay, so let’s move ahead and write another function that would take two integers as parameters, or “arguments”, and returns their multiplication. Can you do it by yourself?

You: Let me try.

Barney: Wow! You’re a real fast learner! Well done! Let’s put this in a complete programme, too. This time we will put the function at the end of the programme. This is done usually because if there are a lot of functions, it would become difficult to read the code. However, even if you place your functions at the end, you must mention their “signatures” at the beginning of the programme.

You: Signatures?

Barney: Yes, the headers of the functions. Take a look at this example:

You: Ah, I get it. By the way you put a semicolon to the end of the signature?

Barney: Yes, that’s because we’re only declaring how the function behaves at the signature. Also note that unlike in the previous example, we have called the function in the same line we are printing it. This does not actually matter: we need not assign the return value of the function to a variable because we’re not gonna use that again.

You: Okay.

Barney: So let’s write another programme with more than one function and using floating point numbers.

You: Mmm, yes, but still I’m not confident about the variables thing here. Why do you have “double r” declared at main and both the other two functions?

Barney: That’s because once you declare a variable inside a function, it becomes “local” to that function, that is the other functions cannot access that variable. The three r’s in the three functions (you know, main is a function, too) are different. However, we could have used a single r in the whole programme like this:

Here, r is a global variable because it’s been declared outside the functions, so it can be used by all the functions.

You: So why don’t we always declare global variables? Doesn’t it easier than declaring them over and over again?

Barney: Well, it may seem that using global variables is quite easy, but that is not actually the case. Let’s see why next time.

You: Okay, thanks Barney! Bye!

Barney: Bye, Ted!



Engineer. Loves tic-tac.


Leave a Reply