C++C++11C++14C++17Introduction to C++Learn C++Templates

Learn About C++ Functions

Functions, also known as Procedures or Sub-ProgramsSub-Algorithms, allow to structure programs in segments of code to perform tasks. A Function is a group of statements that has function a name and can be called with parameters to operate, functions can be called from some point of the program or they can call themselves recursively. All C++ programs have a main() function which holds the main program, we can start all subprograms inside, and all these subprograms are called as a function.

We can group our lines of code into separate statements to do different tasks. We can define functions by specifying a name and and a set of () parenthesis, after this definition, all the statements in this function are listed inside { } braces. How we divide up code among different functions is up to the programmer, logically each function should perform a specific task.

We can create a function without parameters or with parameters, it may return us some results or not. Here is a simple function below, without any parameters and it has no returns,

when naming a function we can NOT use spaces and we can not start with numbers, most of punctuation characters are NOT allowed, generally we use _ character as a space and we can add numbers to the end, like this,

Note that C & C++ programming language is case sensitive, so be careful when using upper cases. In general, most basic standard library functions and user-defined functions are fully lower case and functions that are used by third party libraries, APIs, some modern C++ libraries has functions that start with Upper case and continues with lowercase words.

Let’s see a simple function. Think that we have an application, and at some point, we want to give information about our application. Let’s name the function info() and let’s define as below,

We can call this function in the main() function by calling its name with ; at the end.

This is a very simple C++ example;

as you see we defined our info() function before the main() function. We can define it after the main functions, to do this we must declare this function with its parameters. Please see future posts about declaring functions and using parameters.

Get started building powerful apps with C++Builder!


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is Deleted Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?