Understanding how Functions work in C++

In computer science, a function is a set of statements that together perform an action. The most important function in a C++ program is main().


int main(){}

When you run an app, the computer automatically looks for main() and executes the statements within its body. For example, main() can have several functions such as run, walk, stop that will execute when the app is open:

 

int main(){
    run(); //call function run
    walk(); //call function walk
    stop(); //call function stop
}

Note: The double slash, //, defines the beginning of a comment. A comment is for readability only. Your program ignores them.

In C++, a function consists of a:

  • Return type: The value a function may return. For example, it can return an integer, string, etc. A function may also return Nothing.
  • Arguments: Input data to the function. The input data can be an integer, string, boolean, etc.
  • Function Name: The actual name of the function.
  • Function Body: A collection of statements that define what the function does.

Function Declaration

Before main() can call a function, the function must be declared. A declaration introduces a function to the program. A function declaration provides the function's name, return type, and parameters.

In a function declaration, the return type comes before the function's name. The parenthesis after the name encloses the function's arguments.

For example:

a) The return type of the function below is an int (integer). The name of the function is add. And it has two arguments x and y of type int.


int add(int x, int y);

b) The return type of the function below is void. Meaning the function does not return any value. The name of the function is print. And its argument is of type string.


void print(string x);

c) The return type of the function below is void. Its name is run, and it does not contain any arguments:


void run();

Function Definition

Whereas a declaration provides the function's return type, name, and arguments, a definition provides the set of tasks in a function's body. For example:


int add(int x, int y){
    int z=x+y;  //add x and y and store the value in z
    return z;  //return the value in z
}

Putting it all together

As stated before, a main() function will execute the statements within its body. Before a function is called by main(), the function must be declared.

For example, in the code snippet below, the printHello() function is declared in line 1. The main() function starts at line 2 and it calls the printHello() function (see line 3). The printHello function is defined in line 4.


//1. function declaration comes before the main() function
void printHello();

//2. main function starts
int main(){

    printHello(); //3. call function printHello()

    return 0;
}

//4. function definition
void printHello(){
    std::cout<<"Hello World!"; //print "Hello World!"
}

The code snippet above provides the general template of any program in C++. Next, you will learn about data types and variables in C++.

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.