Lesson 3: Creating, calling and using functions in C++

If you need to perform specific actions in C++ then you call a function. Once a specific function returns the rest of the program resumes from where it was prior to calling the function.
Here is a basic example of what happens when a function is called and the program continues.

The return of this program once compiled and ran is pretty much as you assume:

The function basicFunction() is defined near the top of the code. You can see what it is supposed to do, which is print out a simple sentence. The function itself actually will do nothing and sit dormant until it is called. We call basicFunction() in the main function of our program.
The order of process in this small program is very simple and easy to follow.

  1. main() function is called first as is always the case in a C++ application / program.
  2. “Start Inside Main Function” is printed to the screen.
  3. basicFunction() is called
  4. “Perform action inside the basic function\n” is printed to the screen
  5. basicFunction() is finished and returns back to the main() function
  6. “End Inside Main Function\n” is printed to the screen
  7. main() returns 0 to instruct the operation system that the program has finished running

Once it is explained in simple black and white terms it appears very simple. Indeed it is.

Using functions

Functions either return a value or nothing (void). Lets create a small function to add 2 numbers together. This is a great example as it will have to return a number and also take in 2 arguments

I’ve also added a few new lines into main to illustrate again what is being run at which points and to print to the screen as much informative information as possible.

Compiling and running this program outputs:

A few notes, adding the word int before a function informs the program that this function will return an integer value upon completion. we can replace this return type with many data types such as double, float, char, bool etc. If you would like to see a cheat sheet of C++ data types then Click Here

I’d hope that most of the code above is pretty self explanatory, but anyway I will explain it now.
Function main is called from the operation system. A few lines of text are printed to the screen followed by a different 3rd line. The reason this line is different is that whilst printing to the screen we also call the Add() function, all in one line. Normally this isn’t good practice but for small compact little programs such as this then there really isn’t a reason not to do it. In fact it almost makes the code easier to read.
The Add() function is called but it includes 2 values within its brackets. 5 and 3. These are called arguments and are the values passed into the Add function that will be used to execute the addition.
Add() prints to the screen to inform us what values have been passed through to it and then returns x + y. At this point you will have noticed the x is the first value in the Add() arguments and y is the second. To return a simple addition such as this you simply do x + y, which is exactly what you see at the end of the Add() function.
A few more lines printed to the screen to inform you at what stage the program is at and we’re done.

Some of you eagle eyed people out there may have notice the intermittent use of the word endl and the end of some of my lines. This is the same as using a new line character “\n”. In this small program I switched between them to show you that there really is no difference and its down to personal preference. endl is simple short for end line.

Hopefully this lesson has been easy to follow. Any advice or tips are greatly received. Hopefully as this content and website grows my writing and explanations will become more descriptive and professional. But for now you’re stuck with this. Feel free to comment below and I will reply to any questions you have.

EXERCISE:

If you’d like to extend this little program further, try creating 3 other functions. One for subtract/minus, one for multiplication and one for division.

HINT:

For some of these functions you may need to return numbers with decimal places. For these you need to use a float return type instead of int

Leave a Reply

Your email address will not be published. Required fields are marked *