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.
using namespace std;
//Basic function to demonstrate calling a function
cout << "Perform action inside the basic function\n";
cout << "Start Inside Main Function\n";
cout << "End Inside Main Function\n";
The return of this program once compiled and ran is pretty much as you assume:
Start Inside Main Function
Perform action inside the basic function
End Inside Main Function
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.
- main() function is called first as is always the case in a C++ application / program.
- “Start Inside Main Function” is printed to the screen.
- basicFunction() is called
- “Perform action inside the basic function\n” is printed to the screen
- basicFunction() is finished and returns back to the main() function
- “End Inside Main Function\n” is printed to the screen
- 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.
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
int Add(int x, int y)
cout << "Inside Add(), received " << x << " and " << y << endl;
return (x + y);
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.
cout << "Im in main()\n";
cout << "Im calling Add()\n";
cout << "The Value returned from Add() is: " << Add(5,3) << endl;
cout << "Back in main()\n";
cout << "Exiting Program..";
Compiling and running this program outputs:
Im in main()
Im calling Add()
Inside Add(), received 5 and 3
The Value returned from Add() is: 8
Back in main()
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.
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.
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