What is a Function in C++?

A function in C++ is the grouping together of statements under a common name. Basically, you define a function and write statements within that function. You can call those statements within that function by calling on the name of the function itself. As you may already be aware, every C++ program requires a function called “main”. The “main” function is where your code will execute from in a source file. What this means is that your program will execute code based on what you type in the “main” function, even if it is other functions.

Why use Functions?

Functions can be used in a number of different ways and are used extensively in C++ for practical and organizational reasons. Let’s think about an example. Suppose we want to write a game program where a character moves, has an inventory system, and has a combat system. While you could just code all of the logic for each of these actions straight into your “main” function, what if instead we made a function for each of these actions? In doing so, we would be able to call the functions we created from the main function itself. An example of what a program with this setup could look like is below.

#include <iostream>
...
void inventorySystem(){ //inventory function named
code for inventory system;
}

movementSystem(){ //movement function named
code for our movement system;
}

void combatSystem(){ //combat function named
code for combat system;
}

int main(){
...
inventorySystem(); //calling the inventory function
movementSystem(); //calling the movement function
combatSystem(); //calling the combat function
...
}

As we can see we are able to code the logic for our different systems in our game separately then call them in the main function. Note that you can call another function from any function as long as that function is defined before it is called.

What are the Different Kinds of Functions?

There are two types of functions in C++. A type that has a return value and a type that does not. As you may well know, the main function in C++ should be declared an int type function. By writing “int” before the function name you are telling your computer that you want the program to return an int value. This is required for the main function as the return of a value here signifies that your program has successfully executed to the end. The main function is the only value return type function that doesn’t need a set return value as it will usually default and return a value of “0” regardless of if a value is specified.

A function with a return value must have the type of return value set while declaring the function. The basic syntax for declaring a return type function is shown below:

return-type functionName(set parameters){
function statements;
return return-type-value; //whatever you set your return type as
}

The void type on the other hand does not need a specified as it will lack a return value. If you are familiar with the return statement you might already be aware that the int type function requires the return statement to be accompanied by an integer value while the void does not.

Basic syntax for void functions is shown below:

void functionName(set parameters){
function statements;
return; //no return value
}

In addition both of these types can have what is called “parameters”.

Function Parameters and Arguments

Function parameters are where you set what variables are in your function. The differ from arguments since arguments are the value that those parameters hold. Let’s look at an example of a C++ program where an addition calculator is created with an input for 2 values.

#include <iostream>
#include <cmath>

int a_value, b_value;

int addition(int a, int b){ //a and b are parameters
    return (a + b); //a and b are parameters
}

int main(){
    std::cout << "Enter value of a: " << std::endl;
    std::cin >> a_value;
    std::cout << "Enter value of b: " << std::endl;
    std::cin >> b_value;
    return addition(a_value, b_value); //a_value and b_value are the function arguments
}

Run this code

As you can see above, a function with an int return type was defined and then called in the main function. The return value of the main function will be the addition of the two input variables “a_value” and “b_value”. For example if we run the code and input 3 and 5 the return value would be the value of 3 + 5 or 8.