January 13, 2021

C++ Functions

Functions are so important that any program you write in C++ must have at least one function. That is the main() function. Therefore you need to clearly understand how it works.

  1. Overview of Function
  2. Function Definition
  3. Function Declaration
  4. Calling a Function
  5. Function Arguments
  6. Default Values of Parameter


1. Overview of Functions

A function is a collection of statements that are grouped together to perform a task. When your program is large, the use of functions makes it more organized.

Two things you need to know about functions are:

  • function declaration – this tell the compiler the function name, the return type as well as the parameters
  • function definition – this provides the actual body of the function. That is the statements that are to be executed by the function.

In C++, there are may inbuilt functions you can just use. For instance, the strcat() function for concatenating two string, sqrt() to find the square-root of a number, pow() etc.


2. Function Definition

To create your own function, you must first define it. The general syntax for function definition is:

return_type function_name( parameters ) {
   function body

The various part of the function definition are explained below.

  • Return Type – this is the data type for the function’s return value. However, for function that does not return any value, the return type is void.
  • Function Name – the actual name of the function. There are some rules for naming functions. For example, a function name cannot be a keyword in C++. Also, function name cannot start with a number. Then certain symbols cannot be part of a function. I think, basically, same rules that applies to naming identifiers in C++
  • Parameters – these are variable placeholders for the values the function would use. These values are passed to the function when the function is called and re called actual parameter or arguments.
  • Function Body – this is the collection of statements that the function would execute

Example: Write a function the takes three numbers and returns the largest. We call it getMax()

See below:

int getMax(int x, int y, int z){
	int max;
	if(x > y && x > z) {
		max = x;
	else if(y > z){
		max = y;
		max = z;
	return max;


3. Function Declaration

A function declaration gives the compiler information about a function as well as how to call it. In function, declaration, the body of the function is not included. However, if a function is being declared, we need to be sure that the definition exists

For the getMax() function, the following are two ways we can declare it.

int getMax(int x, int y, int x);

//the parameter names are not mandatory
//so this is also valid
int getMax(int, int, int);

When do you need function declaration? When you want to call a function defined in another file. In this case, you need to declare the function at the to of the file.


4. Calling a Function

To use a function, you need to call it or invoke it. Once you call a function, the compiler transfers the control  to the function. Therefore, the statements in the body of that function begins executing. Once the execution is completed, the program picks up from the statement after the function call. In other words, control is returned to the main program.

To call a function, simply specify the name of the function. Then in parenthesis, you provide the actual parameters if needed. For instance, lets call our getMax() function from the main program.

int main () {
	int x, y, z;
	cout << "Enter first number: ";
	cin >> x;
	cout << "Enter second number: ";
	cin >> y;
	cout << "Enter third number: ";
	cin >> z;
	cout << "The largest is " << getMax(x, y, z);
        return 0;

Two thing to note:

  • when a function is defined in the same file, then you don’t have to declare it
  • variables in a function are local to that function. So the same variables used in the main function are used in the getMax() function.


5. Function Arguments

As mentioned previously, same variable names are used in the function definition and in the function call. In the function definition they are called “formal parameters” while in the function call, they are “actual parameters” or arguments.

When a function is called, arguments can be passed to it in the following ways:

SNCall type and description
1Call by Value:  This approach copies the actual value of the arguments into the formal parameters of the function. Therefore, changes made to the parameter inside the function body will have no effect on the argument.
2Call by Pointer: This approach copies the address of the arguments into the formal parameters. So, inside the function, the address is used to access the actual argument used in the call. Therefore, changes made to the parameters will affect the argument.
3Call by Reference : This approach copies the reference of the arguments into the formal parameters. So, inside the function, the reference is used to access the actual arguments used in the call. Therefore,  changes made to the formal parameters will affect the arguments.


6. Default Values of Parameters

When a function is defined, default parameter values can be specified. These values would be used as the function argument if none is provided during the function call. To do this, simply assign values along with the parameter declaration. See the example below:

int getMax(int x = 5, int y = 10, int z){
   //body of function;


Leave a Reply

Your email address will not be published. Required fields are marked *