tutorialcup@gmail.com

C++ Functions

Function is a block of statements that performs some operations. All C++ programs have at least one function – function called "main()". This function is entry-point of your program. But you can define a lot of new functions, which will be used in your programs.

Advantages of using functions in a program

  1. You can divide your program in logical blocks. It will make your code clear and easy to understand.
  2. Use of function avoids typing same pieces of code multiple times. You can call a function to execute same lines of code multiple times without re-writing it.
  3. Individual functions can be easily tested.
  4. In case of any modification in the code you can modify only the function without changing the structure of the program.

A function definition can be done in the following way:


return_type FunctionName( list of parameters )
{
	//function’s body
}

Take a look on function "main()":


//include a header file from Standard Library
#include <iostream>
using namespace std;
//the work of the program starts from function called main
int main()
{
	cout << "Hello World" << endl;
	return 0;	
}

Here the return type is int, function name is main, and parameter list is empty. The body of the function is


cout << "Hello World" << endl;
return 0;

In other words, if you want to create your own function – you have to define the following elements:

Function’s return type – what is the result of the work of your function?
Function’s Name – this name will be used when you call your function
Parameters List – what data will be operated in this function?
Function’s body – operations performed in the function.

Let’s see an example of the function. We will write a function, which will return minimum of 2 integer values:


int min(int a, int b)
{
	if (a < b)
		return a;
	else
		return b;
}

In this case the return type is int; the list of parameters is int a, int b; function name is min. And the body of the function is:


if (a < b)
	return a;
else
	return b;

The keyword "return" finishes the execution of the function and returns a value to the code from where this function was called.

To call the function from the main to get minimum of two values we have to act in the following way:


int result;
result = min(10, 1002);
cout << "Minimum of 10 and 1002 is " << result << endl;

The output is:

Minimum of 10 and 1002 is 10

To call a function we have to use its name with parameters in parenthesis


min (10, 1002);

Here, "min" is the name of function and "10, 1002" is the list of parameters for this function.

Let’s take a look on the way we can pass parameters to a function in C++. To understand the result produced by different calls we can create a simple function:


void changeA(int a)
{
	++a;
	cout << "a in function is equal to " << a << endl;
}

This function increments the value of "a" and displays a message about the actual value of "a" in the functions.

The parameters can be passed to function in the following way:

Call by value

In this case we are sending to the function the value of the variable, but not this variable.


//create a variable "a" and assign it a value of 10:
int a = 10;
//diaplay a
cout << " Before calling changeA a is " << a << endl;
//call function to change a:
changeA(a);
cout << " After calling changeA a is " << a << endl;

The output of the program is:

Before calling changeA a is 10
a in function is equal to 11
After calling changeA a is 10

As you can see, the function is not changing "a" from "main".

Call by pointer

In this case you can change the value of the variable from "main" in the function. We will discuss the pointers later and you will be able to understand what a pointer is. In simple words, a pointer is a variable with value of the address of the other variable in the memory.

Call by reference

Calling by reference is a little bit the same as calling by pointer. It affects the value of the variable from the parameter list. There is a possibility to use the default values for the parameters in the parameters list. In this case you can assign a value directly to the parameter in the function definition. For example, if we want to set the default value for the second parameter in the function "min", we can change the function definition in the following way:


int min(int a, int b = 0)

Now we can call this function only with one parameter:


int negative = -100;
cout << min(negative) << "is less than" 0 << endl;

An important note for the using functions in your program:

You have to declare the functions before the function main. For example, function declaration for "min" function is:


int min(int a, int b = 0);

And the function’s definition can be done after "main" function: Take a look on this example:


//include a header file from Standard Library
#include <iostream>
using namespace std;
//function declaration
int min(int a, int b = 0);
void changeA(int a);
int main()
{

	//the work of the program starts from function called main
	return 0;
}
//function definition
int min(int a, int b)
{
	if (a < b)
		return a;
	else
		return b;
}
void changeA(int a)
{
	++a;
	cout << "a in function is equal to " << a;
}

In this case, you have to set the default value only once: in definition or declaration of the function.

C++ Interview Questions


Next > < Prev
Scroll to Top