Functions in C++

In this article, we will learn about Functions in C++.

A function is a segment of code that performs a certain task. A function in C++ is a set of statements with a name that can be called from anywhere in the program.

Why Functions in C++?

Let’s discuss answers to the questions like why do we need functions or why are functions so important. This is so because of the following reasons:

  • Readability: Functions bring modularity to our programs. They make programs more readable.
  • Control: Functions enable easy flow control amongst different parts of a program.
  • Redundancy: Functions reduce the redundancy of our programs. If we want a task to be done at multiple places in the program, we can call a function instead of writing the same code repeatedly.
  • Reusability: Functions increase the reusability of code.
  • Abstraction: Functions support abstraction. We can directly use functions from C++ library without knowing how a function works.
  • Error Handling: It becomes easier to find and debug errors.
  • Time: We can save a lot of time using functions.

Types of Functions in C++

1. Standard Library Functions: Functions predefined in C++ header files.

2. User-defined Functions: Functions defined by the user.

In this article, our focus will be on user-defined functions. We will learn how to define functions and use them in our program.

Defining a Function in C++

We can define a function following the syntax given below.

return_type function_name (parameter1, parameter2, …) {
//statements
}

A function thus has the following parts:

  • return_type: it specifies the data type of the result that function will return.
  • function_name: it is the name given to a function.
  • parameters: it is the list of parameters the function is receiving. Here, we specify the number, type and order of parameters. A function may or may not have parameters.
    A function signature comprises function_name and parameters.
  • body: inside curly braces, we write statements to define the task of the function.

Example of a function definition in C++

void display() {
  cout<< "TechVidvan";
}

Here,

  • The return type is void which means the function does not return any value
  • The function name is display
  • It does not have any parameter

Calling a Function in C++

We need to call or invoke a function wherever we want to use it.

Syntax to call a function with no (void) return type:

function_name (argument1, argument2, …);

Syntax to call a function with a return type:

variable = function_name (argument1, argument2, …);

Here, ‘variable’ is the variable that will store the returned value. And argument1, argument2, … are the parameters that we pass to function.

Example of defining and calling a function in C++

#include <iostream>
using namespace std;

int add(int a, int b) {			//Function definition
  return a+b;
}

int main() {
  int n1 = 5, n2 = 12;
  int result;
  result = add(n1, n2);		//Function call
  cout<<result;
  return 0;
}

Output

17

Note that a C++ program always begins by calling the main function, regardless of the order in which functions are defined. In fact, main is the sole function that is called automatically, and code of other functions is run only if it is directly or indirectly called from main.

Function Parameters in C++

The parameters that we pass to a function during function call are called arguments or actual parameters. In the above example, n1 and n2 are actual parameters.

The local variables in the function definition are called formal parameters. In the above example, a and b are formal parameters.

When we call a function, actual parameters are assigned to formal parameters.

Function Declaration in C++

We can also give function definition after function call. But, in that case, it is necessary to declare the function beforehand. It is also called function prototype.

Syntax:

return_type function_name (type1, type2, …);

Example of Function Declaration in C++

#include <iostream>
using namespace std;

int add(int, int);			//Function declaration

int main() {
  int n1 = 5, n2 = 12;
  int result;
  result = add(n1, n2);		//Function call
  cout<<result;
  return 0;
}

int add(int a, int b) {			//Function definition
  return a+b;
}

Output

17

Observe that we obtain the same output in both ways.

Passing Arguments in C++

While calling a function, we can pass arguments in three ways.

1. Call by value

In call by value (or pass by value), values of arguments are passed which are copied to the formal parameters. Actual parameters and formal parameters are stored in different locations of memory. Hence, modifications done to the formal parameters inside the function are not reflected in the actual parameters outside it.

2. Call by reference

In call by reference (or pass by reference), references of arguments get copied to the formal parameters. Using these references, the actual parameters are accessed from inside the function. Thus, modifications done inside the function get reflected in the actual parameters outside it.

3. Call by Pointer

In call by pointer (or pass by pointer), we pass addresses of variables. Hence, both actual and formal parameters point to the same location. Thus, modifications done to the formal parameters inside the function get reflected in the actual parameters outside it.

Let’s understand with examples:

Example of call by value in C++

#include <iostream>
using namespace std;

void modify(int a) {
    a+=2;
}

int main() {
  int x = 3;
  modify(x);
  cout<<"x = "<<x<<endl;
  return 0;
}

Output

x = 3

Observe that in this case, the value of x remains unchanged.

Example of call by reference in C++

#include <iostream>
using namespace std;

void modify(int &a) {
    		a+=2;
}

int main() {
    int x = 3;
    modify(x);
    cout<<"x = "<<x<<endl;
    return 0;
}

Output

x = 5

Now, the value of x has changed.

Example of call by pointer in C++

#include <iostream>
using namespace std;

void modify(int *a) {
    		*a+=2;
}

int main() {
    int x = 3;
    modify(&x);
    cout<<"x = "<<x<<endl;
    return 0;
}

Output

x = 5

In this case also the value of x changes.

Summary

We covered functions in this article. We learnt how to define and declare a function in C++. You must have observed that every function has a return type. Then, we learnt how to call a function. We also noted the importance of the main function in C++. Finally, we learnt about call by value, call by reference and call by pointer methods of passing arguments to a function. We provided suitable examples for better understanding of the concepts.