Functions in C++

Functions in C++

Function is a piece of code written to perform a specific task. Every C++ program must have at least one function to execute. We have already used main function in the previous tutorials. If a C++ program doesn’t have main function then the program cannot be executed. Apart from main function, we can define other functions as well. When a program is executed then main function is called automatically while other functions are called when required.


C++ function can be defined as below.

return-type function_name(parameters)
funtion’s body



means the type of value to be returned by the function. It is important to note that a function can return a value or not. If a function doesn’t return a value then the return type will be void otherwise it can be any other valid data type.


means the name of the function. It must be valid identifier. Functions are called through their names so every function should have a meaningful name.


this part shows whether a function will receive any parameter or not while being called. We can use empty parenthesis or void keyword within parenthesis to show that the function will not receive any parameter. If a function accepts parameters then they must be shown here along with their names and data types. Data type must be mentioned with every parameter separated by commas.

funtion’s body

this part shows the statements within the body of the function. A function might have one or more than one statements within its body enclosed within curly brackets.

The following example shows that how a function is defined and called.


The output of the above program is as below.

 factorial output

In the above program we can see that there are two functions  main and factorial. The keyword long means that the function factorial will return a long integer value while it will receive an integer value when being called. This value will be stored in variable n. The keyword return is used to get the result back from the function. The statement in line 22 is called function calling. The value of variable ‘a’ is passed to function factorial and the value to be returned from factorial will be stored in variable ‘f’.

It is also important to note that function factorial is defined before function main. If we define the function factorial after the main function then the program will raise the following error during compilation.

error C3861: ‘factorial’: identifier not found

This error means that the identifier factorial is not known to the main function in line 22. In order to avoid this error either write the function factorial before main function or declare factorial within main as below.

long factorial(int);

This is called function declaration. It tells the compiler that a function having name factorial will be called which will accept an integer value and will return a long integer.

Passing Parameters By Value

In the last example parameter was passed by value. Passing parameter by value means that the value of sending parameter (variable ‘a’  in our example) is copied to the receiving parameter (variable ‘n’ in our example) and after that if any changes are made in receiving parameter then the value of sending parameter is not affected.

Passing Parameters By Reference

Sometimes it may be required that if we change the value of receiving parameter then the value of actual parameter should also be changed. For doing so we pass the parameters by reference as shown below.


If we want to send a parameter by reference, then we need to send the address of the variable instead of its value as shown in line 17 above. In line 17, variable ‘a’ and ‘b’ are passed by values where as variables ‘c’ and ‘d’ are passed by reference. The & sign with a variable means that address of this variable will be passed instead of its value. At the receiving end in line 5, first two parameters are received by values and the values are stored in variables ‘x’ and ‘y’ respectively. The last two variable are received by reference and means that variables ‘sum’ and ‘diff’ will refer to the same memory addresses being used by variable ‘c’ and ‘d’ respectively. So if the values of variables ‘sum’ and ‘diff’ are changed then this change is also reflected in variables ‘x’ and ‘y’ as shown in the output of the above code.

 output value

More Related Articles For You

    C++ Tutorial

    C++ Quizzes